Link to home
Start Free TrialLog in
Avatar of HaiFai
HaiFaiFlag for Finland

asked on

vb.net list active directory groups

Hi

I have problem with this code, some how i cant list all groups from domain.
Problem is that i have 70 groups in ad, example 10.100.product and 11.255.product .. etc...
but when im runnign this code it will only show me 42 groups so half of groups are missing, where could be the problem?

Imports System.DirectoryServices
Public Class Form1

    Public Shared Function GetDirectoryEntry() As DirectoryEntry

        Dim dirEntry As New DirectoryEntry("LDAP://domain")
        dirEntry.Username = Nothing
        dirEntry.Password = Nothing
        dirEntry.AuthenticationType = AuthenticationTypes.Secure
        Return dirEntry
    End Function

    Public Shared Function GetDirectorySearch() As DirectorySearcher
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        Return dirSearcher
    End Function

    Private Sub exportfromad_Click(sender As System.Object, e As System.EventArgs) Handles exportfromad.Click
        Dim de As DirectoryEntry = GetDirectoryEntry()
        Dim deSearch As DirectorySearcher = New DirectorySearcher()
        deSearch.SearchRoot = de
        Dim test As String
        Dim count As Integer
        deSearch.Filter = "(&(objectClass=group) (samAccountName=" & "*" & "))"
        Dim results As SearchResultCollection = deSearch.FindAll()
        For Each r As SearchResult In results
            Try
                If r.GetDirectoryEntry.Name.Contains("product") Then
                    count += 1
                    test += r.GetDirectoryEntry.Name & vbCrLf
                End If
            Catch
            End Try
        Next
        MsgBox(test)
        MsgBox(count)
    End Sub
End Class

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tried adding a counter inside your foreach loop to make sure how many times your loop is running? You have an if condition and a silent catch block which may be the issue.
Avatar of HaiFai

ASKER

Thanks for fast reply.

Yes i have tried that and if i take all this off
 Try
                If r.GetDirectoryEntry.Name.Contains("product") Then
                    count += 1
                    test += r.GetDirectoryEntry.Name & vbCrLf
                End If
            Catch
            End Try

Open in new window


debug it and check results.count it will only show me 1000 results we have 1119 groups in ad, so somehow code wont see all groups...
ASKER CERTIFIED SOLUTION
Avatar of HaiFai
HaiFai
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HaiFai

ASKER

default PageSize is 1000 if you have more than 1000 objects it will only find part of groups