List All OUs with an OU

I have a some what large OU struture that i am dealling with and need to be able to list All OUs which are with and OU but no list any OUs that are within the OUs i am looking for. Looking at the diagram below i am trying to only to list the OU's with '<------' next to them.

          OU1 <-------
          OU2 <-------
          OU3 <-------
          OU4 <-------

Attaches is what i have so far and which mostly works, but muiltiple OUs are missing, also i want to be able to have the results sorted A-Z

Thanks in advance!.
Dim ADRootEntry As New DirectoryEntry("LDAP://" & NTLogin.NTDomain,NTLogin.NTUsername,NTLogin.NTPassword)
Dim searcher As New DirectorySearcher(ADRootEntry)
Dim queryResults As SearchResultCollection
Dim result As SearchResult
Dim tblADOUs As DataTable = dsADOUs.Tables("tblOUs")
searcher.Filter = "ObjectClass=organizationalUnit"
queryResults = searcher.FindAll()
For Each result In queryResults
  Dim rwDefaultUser As DataRow = tblADOUs.NewRow()
  rwDefaultUser("canonicalName") = Result.Properties("canonicalName")(0)
  rwDefaultUser("distinguishedName") = Result.Properties("distinguishedName")(0)
  rwDefaultUser("name") = Result.Properties("name")(0)	

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Are you looking for the ADS OU structure?
Do you want this code to be edditted or any other script that can match this requirment?
Morton87Author Commented:
Yes i guess i am look for a way to pul the AD structure but from a specific point in AD. The only requirements i have is the final code needs to be in either VB.Net or C#.Net, and the display of the OU information needs to be selectable like in a Treeview.
in your searcher set the the filter to "objectclass=organizationalunit", return all those results, then iterate over the results.  For each object found, perform the same query but append the latest OU name to the query


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Morton87, hi.

I'm not closely familiar with VB, but I try to help.

1) To limit your search only for entries below SitesOU, use
SearchRoot property of the searcher:


Dim sitesOuRoot As DirectoryEntry
sitesOuRoot= New DirectoryEntry("LDAP://ou=SitesOU," & NTDomain)
searcher.SearchRoot = sitesOuRoot

2) To search only 1 level of sub-entries,
use SearchScope property with value :


Public Enumeration SearchScope
searcher.SearchScope = OneLevel

3) To sort result on server side, use Sort property of the searcher:


Public Enumeration SortDirection
searcher.Sort = New SortOption("canonicalName", Ascending);

Combining all 3 recommendations before FindAll() call should be enough to get correct results in the ascending order.

I'm not a VB master, so there may be syntax errors, but you grasp the idea.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.