简体   繁体   中英

get computer from OU

I have a code to get a list of all the computers within a domain.

Now i need to just get the computers which are within a particular OU and not the rest of the machines.

so here is my code to get all the machines from a domain, this works perfectly fine:

DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectDomain);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.Filter = ("(objectClass=computer)");
        mySearcher.SizeLimit = int.MaxValue;
        mySearcher.PageSize = int.MaxValue;

        foreach (SearchResult resEnt in mySearcher.FindAll())
        {
            //"CN=SGSVG007DC"
            string ComputerName = resEnt.GetDirectoryEntry().Name;
            if (ComputerName.StartsWith("CN="))
                ComputerName = ComputerName.Remove(0, "CN=".Length);
            compList.Add(ComputerName);
        }

        mySearcher.Dispose();
        entry.Dispose();

any suggestions?? thanks.

You just need to add the OU to your directory entry, so instead of taking the root of your domain as being the search path, it takes the domain + OU as being the search path.

See "Enumerating objects in an OU" @ http://www.codeproject.com/KB/system/everythingInAD.aspx

I see from your commments that you're having issues here, so let's put this simply - note that this code isn't tested, but should clarify...

string selectDomain = "CN=myCompany,CN=com";
string selectOU = "OU=LosAngeles,OU=America";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectOU + "," + selectDomain);

That essentially gives you the string of "LDAP://OU=LosAngeles,OU=America,CN=MyCompany,CN=com" as the new directory entry. You must specify the full LDAP path, not just the OU or the domain.

try to use this Directory entry:

DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://OU={0},{1}", ouName, selectDomain));

i tried all the above.. but it did not work... so this is what i tried and it worked.

i understand this is not the best way but its the only way working for me... any suggestion.. thanks

DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectedDomain);
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = ("(objectClass=organizationalUnit)");
            mySearcher.SizeLimit = int.MaxValue;
            mySearcher.PageSize = int.MaxValue;
            foreach (SearchResult temp in mySearcher.FindAll())
            {
                Global.logger.Debug("OU = " + temp.Properties["name"][0].ToString());
                DirectoryEntry ou = temp.GetDirectoryEntry();
                DirectorySearcher mySearcher1 = new DirectorySearcher(ou);
                mySearcher1.Filter = ("(objectClass=computer)");
                mySearcher1.SizeLimit = int.MaxValue;
                mySearcher1.PageSize = int.MaxValue;

                if (temp.Properties["name"][0].ToString() == selectedOU)
                {
                    foreach (SearchResult resEnt in mySearcher1.FindAll())
                    {
                        //"CN=SGSVG007DC"
                        string ComputerName = resEnt.GetDirectoryEntry().Name;
                        Global.logger.Debug("ComputerName = " + resEnt.Properties["name"][0].ToString());
                        if (ComputerName.StartsWith("CN="))
                            ComputerName = ComputerName.Remove(0, "CN=".Length);
                        compList.Add(ComputerName);
                    }
                }

                mySearcher1.Dispose();
                ou.Dispose();
            }

            mySearcher.Dispose();
            entry.Dispose();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM