从欧弄电脑电脑

2023-09-08 13:29:07 作者:-君如薄纸一般的脸

我有一个code以获得一个域内的所有计算机的列表。

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

现在我只是需要得到这是一个特定的OU中的计算机和机器不休息。

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

所以这里是我的code把所有的机器从域,这工作完全正常:

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.

推荐答案

您只需要到该OU增加,而不是把你的域的根目录作为搜索路径到你的目录条目,所以,它需要在域+ OU作为搜索路径。

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.

请参阅OU中的枚举对象@ HTTP:// WWW。 codeproject.com / KB /系统/ everythingInAD.aspx

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

我从你的commments,你有问题,在这里看到的,所以我们把这个简单的 - 注意,这code未经测试,但应该澄清......

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

这基本上是让你LDAP:// OU =洛杉矶,OU =美国,CN = myCompany中,CN = COM的字符串作为新目录条目。您必须指定完整LDAP路径,而不仅仅是OU或域。

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.