Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory List OU's

I have this code currently,

        string defaultNamingContext;

        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
        defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
        rootDSE = new DirectoryEntry("LDAP://" + defaultNamingContext);
        //DirectoryEntry domain = new DirectoryEntry((string)"LDAP://" + defaultNamingContext);

        DirectorySearcher ouSearch = new DirectorySearcher(rootDSE,"(objectCategory=Organizational-Unit)", 
                                 null, SearchScope.Subtree);

        MessageBox.Show(rootDSE.Path.ToString());
        try
        {
            SearchResultCollection collectedResult = ouSearch.FindAll();
            foreach (SearchResult temp in collectedResult)
            {
                comboBox1.Items.Add(temp.Properties["name"][0]);
                DirectoryEntry ou = temp.GetDirectoryEntry();
            }
        }

When i use the debugger i can see that rootDSE.Path is infact pointing to the right place, in this case DC=g-t-p,DC=Local but the directory searcher doesn't find any results. Can anyone help?

like image 992
Stephen Murby Avatar asked May 25 '10 15:05

Stephen Murby


People also ask

How do I get a list of organizational units in Active Directory?

Get a list of all Organizational Units with PowerShell. Run PowerShell as administrator. Get a list of all the OUs in Active Directory. We will make use of the Get-ADOrganizationalUnit cmdlet.

What are OU's in Active Directory?

An organizational unit (OU) is a container within a Microsoft Active Directory domain which can hold users, groups and computers. It is the smallest unit to which an administrator can assign Group Policy settings or account permissions.

How do I get OU details in ad PowerShell?

The Get-ADOrganizationalUnit cmdlet gets an organizational unit (OU) object or performs a search to get multiple OUs. The Identity parameter specifies the Active Directory OU to get. You can identify an OU by its distinguished name or GUID.

How do I export OU structure from Active Directory?

To export the data, launch Active Directory Users and Computers. Navigate to the domain structure of the Organizational Unit you wish to export and click on it. From the menu, select the Export List icon (see Figure 1). At this point, you'll have to choose whether you want a .


1 Answers

Stephen - my bad - for some reason, the search using objectCategory doesn't work.

Even though the objectCategory is displayed as CN=Organizational-Unit, for searching, you still need to use the same value as for the objectClass:

So try to use the filter (objectCategory=organizationalUnit) - that definitely works for me!

UPDATE: in order to get some properties in your search result (in order to display them in the combo box), you need to include those when you create the DirectorySearcher:

DirectorySearcher ouSearch = new DirectorySearcher(rootDSE);
ouSearch.Filter = "(objectCategory=Organizational-Unit)";
ouSearch.SearchScope = SearchScope.Subtree;

ouSearch.PropertiesToLoad.Add("name");
// add more properties if you want to ...

With this, you should definitely be able to grab the temp.Properties["name"][0] and stick it into the combobox's list of items.

I don't really see what you need the line

DirectoryEntry ou = temp.GetDirectoryEntry();

after grabbing the name property .....

like image 168
marc_s Avatar answered Sep 22 '22 17:09

marc_s