Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying Active Directory Question...

Status
Not open for further replies.

mattp14

Programmer
Apr 8, 2005
35
US
Is there a way I can write a search function using DirectoryServices that will return only results located within a specific organizational unit in active directory?

The idea being to write a search function that will return, say, a list of all domain controllers for a domain (they all reside in the Domain Controllers OU).

My main goal is to get a list of all the domain controllers, so if you know of a better way than what i'm trying... that's fine with me!

Thanks :)
 
Have a try at

Code:
        Dim dsesearcher As DirectorySearcher = New DirectorySearcher
        Dim RootDSE As String = dsesearcher.SearchRoot.Path
        Dim OUPath As String
        Dim ObjectsInOU As DirectoryEntries
        Dim MyObject As DirectoryEntry

'You may be able to hardcode this, thus leaving out the dsesearcher and rootdse variable.
        OUPath = RootDSE.Insert(7, "ou=corp,")

        Dim DE As DirectoryEntry = New DirectoryEntry(OUPath)

        ObjectsInOU = DE.Children
        For Each MyObject In ObjectsInOU
            Console.WriteLine(MyObject.Properties("cn").Value)
        Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top