get computer from OU

梦想与她 提交于 2020-01-07 04:16:07

问题


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.


回答1:


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.




回答2:


try to use this Directory entry:

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




回答3:


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();


来源:https://stackoverflow.com/questions/5161157/get-computer-from-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!