USERS IN NESTED GROUPS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Fagermoen

    #1

    USERS IN NESTED GROUPS

    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.Collecti ons.Generic.Lis t(Of String))
    Try
    Dim entry As DirectoryEntry = New DirectoryEntry( ggmGroup)
    Dim searcher As DirectorySearch er = New DirectorySearch er(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 attributeWithRa nge As String
    If Not lastQuery Then
    attributeWithRa nge =
    String.Format(" member;range={0 }-{1}", rangeLow, rangeHigh)
    Else
    attributeWithRa nge = String.Format(" member;range={0 }-*",
    rangeLow)
    quitLoop = True
    End If
    searcher.Proper tiesToLoad.Clea r()
    searcher.Proper tiesToLoad.Add( attributeWithRa nge)
    Dim results As SearchResult = searcher.FindOn e

    If results.Propert ies.Contains(at tributeWithRang e) Then
    For Each obj As Object In
    results.Propert ies(attributeWi thRange)
    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.WriteLi ne("Error! Could not connect to group " &
    ggmGroup & ". - " & ex.ToString)
    End Try
    End Sub


    Regards

    Morten Fagermoen

  • Morten Fagermoen

    #2
    Re: USERS IN NESTED GROUPS

    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.Collecti ons.Generic.Lis t(Of String))

    Try
    Dim entry As DirectoryEntry = New DirectoryEntry( ggmGroup)
    Dim searcher As DirectorySearch er = New DirectorySearch er(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 attributeWithRa nge As String
    If Not lastQuery Then
    attributeWithRa nge =
    String.Format(" member;range={0 }-{1}", rangeLow, rangeHigh)
    Else
    attributeWithRa nge = String.Format(" member;range={0 }-*",
    rangeLow)
    quitLoop = True
    End If
    searcher.Proper tiesToLoad.Clea r()
    searcher.Proper tiesToLoad.Add( attributeWithRa nge)
    Dim results As SearchResult = searcher.FindOn e

    If results.Propert ies.Contains(at tributeWithRang e) Then
    For Each obj As Object In
    results.Propert ies(attributeWi thRange)
    Dim curObject As New DirectoryEntry( "LDAP://" +
    obj.ToString)
    If
    UCase(Split(Spl it(curObject.Pr operties("objec tCategory")(0). ToString,
    ",")(0), "=")(1)) = "GROUP" Then
    GetGroupMembers ("LDAP://" &
    DSName(curObjec t.Properties("s amAccountName") (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
    attributeWithRa nge = 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.WriteLi ne("Error! Could not connect to group " &
    ggmGroup & ". - " & ex.ToString)
    End Try
    End Sub




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


    "Morten Fagermoen" <morten@fagermo en.comwrote in message
    news:uMZmxn28GH A.3264@TK2MSFTN GP04.phx.gbl...
    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.Collecti ons.Generic.Lis t(Of String))
    Try
    Dim entry As DirectoryEntry = New DirectoryEntry( ggmGroup)
    Dim searcher As DirectorySearch er = New
    DirectorySearch er(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 attributeWithRa nge As String
    If Not lastQuery Then
    attributeWithRa nge =
    String.Format(" member;range={0 }-{1}", rangeLow, rangeHigh)
    Else
    attributeWithRa nge =
    String.Format(" member;range={0 }-*", rangeLow)
    quitLoop = True
    End If
    searcher.Proper tiesToLoad.Clea r()
    searcher.Proper tiesToLoad.Add( attributeWithRa nge)
    Dim results As SearchResult = searcher.FindOn e
    >
    If results.Propert ies.Contains(at tributeWithRang e) Then
    For Each obj As Object In
    results.Propert ies(attributeWi thRange)
    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.WriteLi ne("Error! Could not connect to group " &
    ggmGroup & ". - " & ex.ToString)
    End Try
    End Sub
    >
    >
    Regards
    >
    Morten Fagermoen

    Comment

    Working...