Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need vbs code to retrieve user record and the groups the user is a member of

Posted on 2011-03-18
4
Medium Priority
?
643 Views
Last Modified: 2012-05-11
I have attached the following code that finds a user LDAP record by user login name. My code retruns the given name.

I would like to expand this to (1) Return all entries from the user record, (2) show all groups a user is a member of and ACLs

250 points for information that helps me figure out the code, 500 for actual code.
'On Error Resume Next

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000

    
objCommand.CommandText = _
    "<LDAP://dc1.mycompany.local:389/dc=mycompany,dc=local>; (&(objectCategory=User)(givenName=chris));Name;Subtree"

        serverandport=InputBox("please input server:port")
        loginname=InputBox("please input login name")
        root=InputBox("please input root (for example: dc=stjoes,dc=com")
        
        objCommand.CommandText = _
    "<LDAP://" &serverandport & "/"&root&">; (&(objectCategory=User)(samaccountname="&loginname&"));Name;Subtree"

    
Set objRecordSet = objCommand.Execute


If objrecordset.BOF = False Then
	objRecordSet.MoveFirst
	counter = 1
	outstring = ""
	Do Until objRecordSet.EOF
    	outstring = outstring & counter &  ": " & objRecordSet.Fields("Name").Value & vbCrLf
    	objRecordSet.MoveNext
    	counter = counter + 1
	Loop
	MsgBox "result = " & outstring
Else
	msgbox loginname & " not found!"
End If

Open in new window

0
Comment
Question by:Christopher Schene
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Tasmant earned 1000 total points
ID: 35164734
1 return all entries
this is difficult because not all entries are string type.
so for some of them you need additional code in order to translate them (like byte, double ...)
you can find information here to retrieve all attributes for a specific object class.
http://www.rlmueller.net/freecode5.htm
http://gallery.technet.microsoft.com/scriptcenter/4d192f4d-2830-4a3e-9352-64a7e696a36e
you will also find information to convert specific attribute to a more readable format.

2 show a group a user is member of
depends if you want nested group or not. on the same site you will find information to list group membership, with primary group, nested groups.

3 show acl
do you want to see ACL for the LDAP object itself (the username in your input), or do you want to know where the user have access everywhere? the seconat part is quite impossible. you will find examples on the following links
http://technet.microsoft.com/en-us/library/ff405675.aspx
http://technet.microsoft.com/en-us/library/ff406131.aspx
0
 
LVL 8

Assisted Solution

by:GundogTrainer
GundogTrainer earned 1000 total points
ID: 35164740
Add the "memberof" property to properties to be returned, eg.

 "<LDAP://" &serverandport & "/"&root&">; (&(objectCategory=User)(samaccountname="&loginname&"));Name,Memberof;Subtree"

and add the code like :
memberofGroups=objRecordSet.Fields("memberof").Value
for p = 0 to ubound(memberofGroups)
 wscript.echo memberofGroups(p)
next

I dont know how you need the data displaying - the memberof field is held as a distinguised name so if you want this to be the display name then you would need a function to look it up for you.

As for all properties you can would be better to use LDP.exe (from the resource kit) or adsiedit  to inspect the object so you can identify the pro[perties you need.
0
 

Author Comment

by:Christopher Schene
ID: 35185466
incraese points
0
 

Author Closing Comment

by:Christopher Schene
ID: 35185472
Thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question