×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

List Entries In Active Directory

List Entries In Active Directory

List Entries In Active Directory

(OP)
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'.

RE: List Entries In Active Directory

(OP)
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'.

RE: List Entries In Active Directory

(OP)
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'.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close