?
Solved

vb.net list active directory groups

Posted on 2012-04-04
4
Medium Priority
?
754 Views
Last Modified: 2012-04-09
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

0
Comment
Question by:HaiFai
  • 3
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37805414
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.
0
 
LVL 7

Author Comment

by:HaiFai
ID: 37805458
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...
0
 
LVL 7

Accepted Solution

by:
HaiFai earned 0 total points
ID: 37805658
found solution for this problem ... i needed to increase deSearch.PageSize and then it will find all
0
 
LVL 7

Author Closing Comment

by:HaiFai
ID: 37822697
default PageSize is 1000 if you have more than 1000 objects it will only find part of groups
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

840 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