Solved

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

Posted on 2011-03-18
4
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Christopher Schene
ID: 35185466
incraese points
0
 

Author Closing Comment

by:Christopher Schene
ID: 35185472
Thank you
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
A hard and fast method for reducing Active Directory Administrators members.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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