Solved

vb.net list active directory groups

Posted on 2012-04-04
4
518 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
default PageSize is 1000 if you have more than 1000 objects it will only find part of groups
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now