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
Solved

Query Active Directory for OU's VB.Net

Posted on 2014-11-20
6
477 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
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 tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

809 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