[英]get computer from OU
我有一个代码来获取域中所有计算机的列表。
现在,我只需要获取位于特定OU中的计算机,而不是其余计算机。
所以这是我的代码,用于从域中获取所有计算机,这工作得很好:
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();
有什么建议么?? 谢谢。
您只需要将OU添加到目录条目中,因此,无需将域的根作为搜索路径,而是将域+ OU作为搜索路径。
请参阅“枚举OU中的对象” @ http://www.codeproject.com/KB/system/everythingInAD.aspx
我从您的评论中看到您在这里遇到了问题,所以我们简单地讲一下-请注意,此代码未经测试,但应予以澄清...
string selectDomain = "CN=myCompany,CN=com";
string selectOU = "OU=LosAngeles,OU=America";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + selectOU + "," + selectDomain);
这实际上为您提供了字符串“ LDAP:// OU = LosAngeles,OU = America,CN = MyCompany,CN = com”作为新目录条目。 您必须指定完整的LDAP路径,而不仅仅是OU或域。
尝试使用此目录条目:
DirectoryEntry条目=新的DirectoryEntry(string.Format(“ LDAP:// OU = {0},{1}”,ouName,selectDomain));
我尝试了以上所有方法..但是没有用...所以这就是我尝试过的并且可以使用。
我知道这不是最好的方法,但是对我来说是唯一的方法。任何建议..谢谢
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.