Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB LDAP to get a users "member of" groups, within AD

Posted on 2008-09-29
1
Medium Priority
?
5,792 Views
Last Modified: 2012-05-05
HI all,
im working on a script and part of it is to find out the total number of groups a user is a member of, what that group is and what the descript of that group is.

I can count the groups (foundPerm) and also write these groups to an array (arrNames), however i have the following 2 questions:
1) is there a quicker way to do this?
2) How do i get a list in another array (arrNames1), which shows the groups description?

Thanks
''    Const ADS_SCOPE_SUBTREE = 2
''        objConnection.Provider = "ADsDSOObject"
''        objConnection.Open "Active Directory Provider"
''    Set objCommand.ActiveConnection = objConnection
''        objCommand.Properties("Page Size") = 1000
''        objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
''    On Error Resume Next
''    Set oRoot = GetObject("LDAP://RootDSE")
''    strDomain = oRoot.Get("DefaultNamingContext")
    
''    objCommand.CommandText = _
''        "SELECT distinguishedName FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' " & _
''        "AND cn='" & userGBID & "'"
''
''    Set objRecordset = objCommand.Execute
''
''    objRecordset.MoveFirst
''        Do Until objRecordset.EOF
''            result = (objRecordset.Fields("distinguishedName").Value)
''            objRecordset.MoveNext
''        Loop
''
''    Result4 = Right(result, (Len(result)) - (InStr(1, result, ",")))
''
''    strUserName = "Cn=" & userGBID & ", " & Result4 & ""
''    strUserPath = "LDAP://" & strUserName
''    Set objUser = GetObject(strUserPath)
''    intsize = 0
''    foundPerm = 0
''    For Each strGroup In objUser.memberof
''        foundPerm = foundPerm + 1 '<---- number of groups a user is member of
''        strGroupPath = "LDAP://" & strGroup
''        ReDim Preserve arrNames(intsize)
''        Set objGroup = GetObject(strGroupPath)
''        strGroup = Split(strGroup, ",OU=")
''        strGroup = Split(strGroup(0), "CN=")
''        arrNames(intsize) = Trim(strGroup(1)) '<--- array where these groups are saved
''        'arrDescription (intsize)
''        intsize = intsize + 1
''    Next
''    intRow = 1

Open in new window

0
Comment
Question by:jamiepryer
[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
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 22602971
Hi, here's a completely different approach.....

Regards,

Rob.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
Const ADS_SCOPE_SUBTREE = 2
 
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.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.get("defaultNamingContext")
 
objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' AND objectClass='person'"
Set objRecordSet = objCommand.Execute
 
strResults = """distinguishedName"",""Group name"",""Group Description"""
 
If Not objRecordSet.EOF Then
	objRecordSet.MoveFirst
	Do Until objRecordSet.EOF
		WScript.Echo "Enumerating " & objRecordSet.Fields("distinguishedName").Value
		Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)
		Set colGroups = objUser.Groups
		strResults = strResults & VbCrLf & """" & objUser.distinguishedName & """"
		For Each objGroup In colGroups
			strGroupName = objGroup.CN
			strDescription = objGroup.Description
			strResults = strResults & VbCrLf & """"",""" & strGroupName & """,""" & strDescription & """"
		Next
		objRecordSet.MoveNext
	Loop
End If
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("Results.csv", True)
objOutputFile.Write strResults
objOutputFile.Close
MsgBox "Done"

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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