Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Entries In Active Directory

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
I am trying to achieve my first attempt to at reading Active Directory entries into a SearchResultCollection.
At this stage I'm just trying to return a list of the 'cn' property.

I run the code below - with a valid DirectoryEntry address of course.
The code tootles along and returns the 'results' collection with a count of 63.
I have assumed the count means the 63 entries have been returned.

The code then fails on the line:-
MessageBox.Show(result.Properties("cn")(0))

The error message is {"Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index"}.
Any suggestions/help would be welcome.

Code:
Try
            ' Bind to the users container.
            Dim rootentry As New DirectoryEntry("LDAP://.........................................")
            rootentry.Username = Nothing
            rootentry.Password = Nothing


            ' Create a DirectorySearcher object.
            Dim Searcher As New DirectorySearcher(rootentry)
            Searcher.PropertiesToLoad.Add("cn")

            Dim results As SearchResultCollection
            results = Searcher.FindAll()

            Dim result As SearchResult

            For Each result In results
                MessageBox.Show(result.Properties("cn")(0))
            Next


        Catch comEx As System.Runtime.InteropServices.COMException
            Console.WriteLine(comEx)
        Catch invOpEx As InvalidOperationException
            Console.WriteLine(invOpEx)
        Catch ex As Exception
            MsgBox("Error " & vbCrLf & ex.Message)
        End Try

Dazed and confused.

Remember.. 'Depression is just anger without enthusiasum'.
 
I have found a bit more out by trial an error but I am still struggling.
The results return a list of entries but some of the entries seem to be empty/nothing.

results(0).Properties("cn") errors
results(1).Properties("cn") returns a value
results(2).Properties("cn") returns a value
results(3).Properties("cn") errors
results(4).Properties("cn") returns a value
results(5).Properties("cn") errors
..
..
..
etc.

Anybody know why this is and how I check for a dud entry?

Any help would be gratefully received.




Dazed and confused.

Remember.. 'Depression is just anger without enthusiasum'.
 
OK resolved it.

My query listed all entries within the AD rootentry ( computers, groups, whatever...).
When you try and reference user related properties against the result it blows up with property occurrence not found.
Initially I checked to see if the type is a user before adding a filter to my search to limit to just users.

Sticking the code below in after the Dim Searcher line sorts it all out and I can live happily ever after.

Code:
 Searcher.Filter = "(&(objectCategory=User)(objectClass=person))"

Dazed and confused.

Remember.. 'Depression is just anger without enthusiasum'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top