USERS IN NESTED GROUPS

M

Morten Fagermoen

Hi!

I have the function below that return all users in a group, but this code
does not work if there are nested groups.

Can anyone tell me what to change to also get the users in the nested
groups?


Sub GetGroupMembers(ByVal ggmGroup As String, ByRef ggmMembers As
System.Collections.Generic.List(Of String))
Try
Dim entry As DirectoryEntry = New DirectoryEntry(ggmGroup)
Dim searcher As DirectorySearcher = New DirectorySearcher(entry)
searcher.Filter = "(objectClass=*)"
Dim rangeStep As System.UInt32 = 1000
Dim rangeLow As System.UInt32 = 0
Dim rangeHigh As System.UInt32 = CUInt(rangeLow + (rangeStep -
1))
Dim lastQuery As Boolean = False
Dim quitLoop As Boolean = False

Do
Dim attributeWithRange As String
If Not lastQuery Then
attributeWithRange =
String.Format("member;range={0}-{1}", rangeLow, rangeHigh)
Else
attributeWithRange = String.Format("member;range={0}-*",
rangeLow)
quitLoop = True
End If
searcher.PropertiesToLoad.Clear()
searcher.PropertiesToLoad.Add(attributeWithRange)
Dim results As SearchResult = searcher.FindOne

If results.Properties.Contains(attributeWithRange) Then
For Each obj As Object In
results.Properties(attributeWithRange)
ggmMembers.Add(obj.ToString)
Next
Else
lastQuery = True
End If
If Not lastQuery Then
rangeLow = CUInt(rangeHigh + 1)
rangeHigh = CUInt(rangeLow + (rangeStep - 1))
End If
Loop While Not quitLoop
Catch ex As Exception
Console.WriteLine("Error! Could not connect to group " &
ggmGroup & ". - " & ex.ToString)
End Try
End Sub


Regards

Morten Fagermoen
 
M

Morten Fagermoen

Hi, again!

I made it work myself. So to you other newbies out there, here is the code:

Public Sub GetGroupMembers(ByVal ggmGroup As String, ByRef ggmMembers As
System.Collections.Generic.List(Of String))

Try
Dim entry As DirectoryEntry = New DirectoryEntry(ggmGroup)
Dim searcher As DirectorySearcher = New DirectorySearcher(entry)
searcher.Filter = "(objectClass=*)"
Dim rangeStep As System.UInt32 = 1000
Dim rangeLow As System.UInt32 = 0
Dim rangeHigh As System.UInt32 = CUInt(rangeLow + (rangeStep -
1))
Dim lastQuery As Boolean = False
Dim quitLoop As Boolean = False
Do
Dim attributeWithRange As String
If Not lastQuery Then
attributeWithRange =
String.Format("member;range={0}-{1}", rangeLow, rangeHigh)
Else
attributeWithRange = String.Format("member;range={0}-*",
rangeLow)
quitLoop = True
End If
searcher.PropertiesToLoad.Clear()
searcher.PropertiesToLoad.Add(attributeWithRange)
Dim results As SearchResult = searcher.FindOne

If results.Properties.Contains(attributeWithRange) Then
For Each obj As Object In
results.Properties(attributeWithRange)
Dim curObject As New DirectoryEntry("LDAP://" +
obj.ToString)
If
UCase(Split(Split(curObject.Properties("objectCategory")(0).ToString,
",")(0), "=")(1)) = "GROUP" Then
GetGroupMembers("LDAP://" &
DSName(curObject.Properties("samAccountName")(0).ToString), ggmMembers)
End If
ggmMembers.Add(obj.ToString)
Next
Else
lastQuery = True
End If
If Not lastQuery Then
rangeLow = CUInt(rangeHigh + 1)
rangeHigh = CUInt(rangeLow + (rangeStep - 1))
End If
attributeWithRange = Nothing
results = Nothing
Loop While Not quitLoop

entry = Nothing
searcher = Nothing
rangeStep = Nothing
rangeLow = Nothing
rangeHigh = Nothing
lastQuery = Nothing
quitLoop = Nothing

Catch ex As Exception
Console.WriteLine("Error! Could not connect to group " &
ggmGroup & ". - " & ex.ToString)
End Try
End Sub




******************************************************************************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top