Solved

Query Active Directory for OU's VB.Net

Posted on 2014-11-20
6
517 Views
Last Modified: 2014-11-20
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
Comment
Question by:AnthonySmithMCP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 500 total points
ID: 40455891
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
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40455955
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
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40455982
It's pointing to this line

Untitled.png
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40456003
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
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40456015
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
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40456026
nvm got it.. lol just needed to use SearchScope.OneLevel
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

724 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