Solved

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

914 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

18 Experts available now in Live!

Get 1:1 Help Now