Solved

Query Active Directory for OU's VB.Net

Posted on 2014-11-20
6
427 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
  • 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
 
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

Join & Write a Comment

Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now