Solved

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

Posted on 2011-03-18
4
620 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I know all systems administrator at some time or another has had to create a script to copy file from a server share to a desktop. Well now there is an easy way to do this in Group Policy. Using Group policy preferences is not hard. The first thing …
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now