Solved

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

Posted on 2011-03-18
4
626 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:cschene
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Tasmant earned 250 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 250 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:cschene
ID: 35185466
incraese points
0
 

Author Closing Comment

by:cschene
ID: 35185472
Thank you
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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