enumerate the membership of a large AD group

jave

New member
Joined
Mar 5, 2010
Messages
4
Programming Experience
Beginner
Hi All.

I'm pretty new to VB.Net.
I need to enumerate the membership of a large AD group (>1500 members).

Found an working example in C#
Enumerating Members in a Large Group
but I failed to translate it in VB.net

I'm looking for help. Any assistance would be appreciated.

jave
 
Here is a source code example I have written a few years back. It may need some updates at this time (I know I've certainly updated my originally version of it). If you still need help let me know.

Active Directory
 
Hi Tom,
I've double checked your demo code.
Enumerating Security Groups works well, but only with groups < 1500 members.
It returns nothing with lager groups.

I've figured out how to do with large groups. (See code below)
But this code returns only the adsPath, but what I want is samaccountname and the mail property.

Does anyone have a idea??
BR Jave



Dim entry As New DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com")
Dim searcher As New DirectorySearcher(entry)
searcher.Filter = "(objectClass=*)"

Dim rangeStep As UInteger = 1000
Dim rangeLow As UInteger = 0
Dim rangeHigh As UInteger = 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)
End If
searcher.PropertiesToLoad.Clear()
searcher.PropertiesToLoad.Add(attributeWithRange)
Dim results As SearchResult = searcher.FindOne()
For Each res As String In results.Properties.PropertyNames
System.Diagnostics.Debug.WriteLine(res.ToString())
Next
If results.Properties.Contains(attributeWithRange) Then
For Each obj As Object In results.Properties(attributeWithRange)
Console.WriteLine(obj.[GetType]())
Console.WriteLine(obj.ToString())
Next
If lastQuery Then
quitLoop = True
End If
Else
lastQuery = True
End If
If Not lastQuery Then
rangeLow = rangeHigh + 1
rangeHigh = rangeLow + (rangeStep - 1)
End If
Loop While Not quitLoop
 
Ok. I got it.
:D
VB.NET:
    Sub FillListbox(ByRef ListBox As ListBox, ByVal OU As String)
        Try
            Dim i As Integer = 0
            Dim entry As New DirectoryEntry("LDAP://" & OU)
            Dim searcher As New DirectorySearcher(entry)

            searcher.Filter = "(objectClass=*)"
            Dim rangeStep As Integer = 1500
            Dim rangeLow As Integer = 0
            Dim rangeHigh As Integer = 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)
                End If
                searcher.PropertiesToLoad.Clear()
                searcher.PropertiesToLoad.Add(attributeWithRange)
                searcher.PropertiesToLoad.Add("samaccountname")
                searcher.PropertiesToLoad.Add("CN")

                Dim results As SearchResult = searcher.FindOne()

                If results.Properties.Contains(attributeWithRange) Then
                    For Each obj As Object In results.Properties(attributeWithRange)
                        Dim DirEntry As New DirectoryEntry("LDAP://" & obj.ToString())
                        ListBox.Items.Add(DirEntry.Properties("samaccountname").Value & " | " & DirEntry.Properties("cn").Value)
                    Next

                    If lastQuery Then
                        quitLoop = True
                    End If
                Else
                    lastQuery = True
                End If

                If Not lastQuery Then
                    rangeLow = rangeHigh + 1
                    rangeHigh = rangeLow + (rangeStep - 1)
                End If

            Loop While Not quitLoop


        Catch ex As Exception
            MessageBox.Show("Error " & ex.Message, "ERROR Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
 
Last edited:
Back
Top