• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3985
  • Last Modified:

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.

Domain
      SitesOU
          OU1 <-------
          OU2 <-------
             OU2-1
             OU2-2
          OU3 <-------
             OU3-1
          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.PropertiesToLoad.Add("distinguishedName")
searcher.PropertiesToLoad.Add("canonicalName")
searcher.PropertiesToLoad.Add("name")
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)	
  tblADOUs.Rows.Add(rwDefaultUser)				
Next

Open in new window

0
Morton87
Asked:
Morton87
2 Solutions
 
bsharathCommented:
Hi,

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?
0
 
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.
0
 
MeviCommented:
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

0
 
NopiusCommented:
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:
http://msdn2.microsoft.com/en-us/library/system.directoryservices.directorysearcher.searchroot.aspx
http://msdn2.microsoft.com/en-us/library/system.directoryservices.directoryentry.directoryentry.aspx

Usage:

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 :
http://msdn2.microsoft.com/en-us/library/system.directoryservices.directorysearcher.searchscope.aspx
http://msdn2.microsoft.com/en-us/library/947c7t76.aspx

Usage:

Public Enumeration SearchScope
...
searcher.SearchScope = OneLevel
...

3) To sort result on server side, use Sort property of the searcher:
http://msdn2.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sort.aspx
http://msdn2.microsoft.com/en-us/library/system.directoryservices.sortdirection.aspx

Usage:

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.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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