none
Retrieving all users from AD using VB.NET RRS feed

  • Question

  • I am using VB.NET and searching AD for all users under a certain OU.  I user ADUC and it shows 5482 and when I run my progran, under elevated or normal privileges, it only returns 5476.  I am missing 6 names.  And one of the names is mine that I know is in the OU.

    I have tried the following filters:

    'objSearchADAM.Filter = "(&(objectCategory=person)(objectClass=user))"
    'objSearchADAM.Filter = "(&(objectCategory=User)(objectClass=person))"
    'objSearchADAM.Filter = "(&(objectClass=user))"
    'objSearchADAM.Filter = "(&(objectClass=person))"

    They all return the same number of entries.

    Any ideas why I am not seeing all the accounts?

    Thanks,

    Dave


    Dave

    Thursday, March 13, 2014 5:25 PM

Answers

  • Correct, this is not a scripting question. Please re-post your question in the VB.NET forum.

    When you post there, make it clear that your question is about how to properly populate the list box with the names you're receiving. If your LDAP filter works correctly in ADUC, then the problem is not with the filter but with how you're populating your list box.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 8:14 PM
    Moderator

All replies

  • Since we don't have access to your network or all of the code you're using, there's really no way for us to know the answer to your question.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 5:34 PM
    Moderator
  • I can post the code if you like.  I was just wondering if my filters were wrong and that is why I am not getting all the users

    Public Function GetUsers() As List(Of String)
            Dim objADAM As DirectoryEntry                   ' Binding object.
            Dim objGroupEntry As DirectoryEntry             ' Group Results.
            Dim objSearchADAM As DirectorySearcher          ' Search object.
            Dim objSearchResults As SearchResultCollection  ' Results collection.
            Dim strPath As String                           ' Binding path.
            Dim result As New List(Of String)

            Dim i As Integer 'Used to cont the number of groups retrieved.
            i = 0

            ' Construct the binding string.       
            strPath = "LDAP://dc=dev,dc=company,dc=net"

            'Try
            ' Get the AD LDS object.
            objADAM = New DirectoryEntry(strPath)
            objADAM.RefreshCache()

            ' Get search object, specify filter and scope,
            ' perform search.
            objSearchADAM = New DirectorySearcher(objADAM)
          
           objSearchADAM.PageSize = 1000
          
           'objSearchADAM.Filter = "(&(objectCategory=person)(objectClass=user))"
            objSearchADAM.Filter = "(&(objectCategory=User)(objectClass=person))"
            'objSearchADAM.Filter = "(&(objectClass=user))"
            'objSearchADAM.Filter = "(&(objectClass=person))"
           
            objSearchADAM.SearchScope = SearchScope.Subtree
            objSearchResults = objSearchADAM.FindAll()

            ' Catch ex As Exception
            '    MsgBox(ex.Message)
            ''''Throw e
            'End Try

            Dim objResult As SearchResult

            For Each objResult In objSearchResults

                Application.DoEvents()

                objGroupEntry = objResult.GetDirectoryEntry
                result.Add(objGroupEntry.Name)

                Dim reschar() As Char = {"C", "N", "="} 'Added to trim the CN= off the front of the string
                resname = ""
                resname = (objGroupEntry.Name)

                Dim reschar2 As String = "\"
                resname = (resname.TrimStart(reschar)) '(objGroupEntry.Name)
                ResName = Replace(ResName, reschar2, "")

             
                ListBox1.Items.Add(ResName.TrimStart(reschar))
                ListBox1.Refresh()
                Label1.Text = "Number of Users found: " & i 'ListBox2.Items.Count '& " GetUsers Listbox" '"Once the members are listed, click clipboard button to export:"
                Label1.Refresh()
                i = i + 1
            Next objResult
            ' Else
            ' Throw New Exception("No users found")
            ' MsgBox("No users Found!")
            ' End If

            MsgBox("All Done!", vbOKOnly, "Attention:")

            Return result

        End Function


    Dave

    Thursday, March 13, 2014 5:38 PM
  • You should also be able to copy and paste the LDAP query into the ADUC "custom search" window to check whether your query is correct.

    Other than that: Since we don't have access to your network, nor can we see your screen, there's really no way for us to answer the question.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 5:45 PM
    Moderator
  • I would also note that this is a script forum and you are asking a VB.Net question.

    You also need to check count like this to match ADUC.

    objSearchResults = objSearchADAM.FindAll()
    MsgBox objSearchResults.Count

    Your processing may well be miscounting and none of use want to check you math.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 6:56 PM
  • This is the simplest code to get the parallel count:

    As Bill suggested paste the exact same filter into ADUC and be sure you add the OU restriction.

    Public Sub CountUsers()
    
             Dim Searcher As DirectorySearcher = New DirectorySearcher("(&(objectCategory=person)(objectClass=user))")
             searcher.SearchBase="LDAP://dc=dev,dc=company,dc=net"
             searcher.PageSize = 1000
             Dim results As SearchResultCollection = searcher.FindAll()
             MsgBox results.Count
             
    End Function
    


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 7:04 PM
  • Here is the identical code in PowerShell:

    $searcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user))'
    $searcher.SearchBase='LDAP://dc=dev,dc=company,dc=net'
    $searcher.PageSize=1000
    $results=$searcher.FindAll()
    $results.Count


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 7:07 PM
  • Not so much that the count is off but that the acocunts that I know are in there are not showing up in the ListBox.  My last name starts with C. However, the list only return 3 names with the last name starting with C when there are like 30-40 that start with C. 

    I will continue to look at it.

    Thanks,

    Dave


    Dave

    Thursday, March 13, 2014 7:53 PM
  • Try your LDAP query in the ADUC console.

    It sounds like the problem isn't with your query but how you're displaying your results.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 7:57 PM
    Moderator
  • Not so much that the count is off but that the acocunts that I know are in there are not showing up in the ListBox.  My last name starts with C. However, the list only return 3 names with the last name starting with C when there are like 30-40 that start with C. 

    I will continue to look at it.

    Thanks,

    Dave


    Dave

    Now you are changing the question completely.  We need to move you to the VB.Net forum.   From what you have posted so far there is no way to understand what you are asking or what is causing your problem.  I suspect you have some issues with how to use VB.Net from looking at the code you posted which seems to be a hybrid of VBScript VB.Net with some missing knowledge of how to use VB.Net.  As a result you are thinking that the filter is your issue.  This is impossible assuming the filter in ADUC and the one I posted are identical.

    You will need to post all of your code in the VB.Net forum so they can try and figure out where your logic error or understanding od AD is going off course.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 8:02 PM
  • I am starting to think that myself.  I am trying to see if a ListBox has any size or list number restrictions.  Might another control work better to put the names into as they are retrieved.  ADUC give me the same number as my application but it displays all the names.  That is why I am wondering if there is a restriction or issue using a listbox control.

    Dave


    Dave

    Thursday, March 13, 2014 8:03 PM
  • Bill - move this to VB.Net forum.  We can keep working on it but it is not a scripting issue.

    Dave - try Bill's suggestion.  I believe that if you see how the numbers come up you will be forced to look deeper.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 8:04 PM
  • I am starting to think that myself.  I am trying to see if a ListBox has any size or list number restrictions.  Might another control work better to put the names into as they are retrieved.  ADUC give me the same number as my application but it displays all the names.  That is why I am wondering if there is a restriction or issue using a listbox control.

    Dave


    Dave

    List is limited 0- when it is - to 32K of items.  You are not at the end.  It is something in you understanding or the logic.  With what you have posted it is not possible to tell.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 8:06 PM
  • Correct, this is not a scripting question. Please re-post your question in the VB.NET forum.

    When you post there, make it clear that your question is about how to properly populate the list box with the names you're receiving. If your LDAP filter works correctly in ADUC, then the problem is not with the filter but with how you're populating your list box.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 8:14 PM
    Moderator
  • Got thanks.  Will repost to VB.NET as soon as I can figure out how to get to it.  Always had navigation issues with these forums.

    Thanks for looking at it.

    Dave


    Dave

    Thursday, March 13, 2014 8:15 PM
  • The following is all you need to load a listbox with string returned from the directory searcher.

    Public Sub GetUsers()
        
        Dim searcher As DirectorySearcher = New DirectorySearcher("(&(objectCategory=person)(objectClass=user))")
        searcher.SearchBase = "LDAP://dc=dev,dc=company,dc=net"
        searcher.PageSize = 1000
        Dim results As SearchResultCollection = searcher.FindAll()
        
        For Each result as SearchResult In results
            Application.DoEvents()
            ListBox1.Items.Add(result.Properties("samaccountname"))
            ListBox1.Refresh()
        Next
        
        Return result
    
    End Sub
    

    Start with samname then reformat the string when you know this works.


    ¯\_(ツ)_/¯

    Thursday, March 13, 2014 8:18 PM
  • Thursday, March 13, 2014 8:20 PM
    Moderator