[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 700
  • Last Modified:

Query Active Directory for OU's VB.Net

I'm trying to find some code that will help me query active directory and get a list of organizational units. I have some other code i was using to get the computer objects.. Now I'm trying to recode this to get OU's.  Can someone please help?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Clears listbox
        results.Items.Clear()

        'Connect to AD Database
        Dim rootEntry As New DirectoryEntry("GC://DC=TonyNet,DC=org", Domain.Text + userID.Text, password.Text)

        'Set What you want to search for
        Dim searcher As New DirectorySearcher(rootEntry)
        searcher.PropertiesToLoad.Add("cn")
        searcher.PropertiesToLoad.Add("mail")

        'Set Your Search Filter
        searcher.Filter = "(objectCategory=Computer)"

        'Finds all
        Dim queryResults As SearchResultCollection
        queryResults = searcher.FindAll()

        'Puts the results into a list Box
        Dim result As SearchResult
        For Each result In queryResults
            results.Items.Add(result.Properties("cn")(0))
        Next

End Sub

Open in new window

0
AnthonySmithMCP
Asked:
AnthonySmithMCP
  • 4
  • 2
2 Solutions
 
Nick67Commented:
It may be really simple.
Change you filter from
       'Set Your Search Filter
        searcher.Filter = "(objectCategory=Computer)"

to
       'Set Your Search Filter
        searcher.Filter = "(objectCategory=organizationalUnit)"


Try that, and let me know
0
 
AnthonySmithMCPAuthor Commented:
Thank you for your help. When i run it.. I get a message saying "Index was out of range. Must be non-negative and less than the size of the collection. Perimeter name index"
0
 
AnthonySmithMCPAuthor Commented:
It's pointing to this line

Untitled.png
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Nick67Commented:
Your properties to load will need to change
       searcher.PropertiesToLoad.Add("cn")
        searcher.PropertiesToLoad.Add("mail")


These are VERY unlikely to be applicable to an OU :)
And because they aren't, you have results, but no applicable properties to punch into the listbox.
Hence the error

What OU properties do you want listed?
0
 
AnthonySmithMCPAuthor Commented:
Yep.. you're right, I needed to change the property to "name".  Is it anyway to change the search to NOT go to sub OU's?
0
 
AnthonySmithMCPAuthor Commented:
nvm got it.. lol just needed to use SearchScope.OneLevel
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now