Active directory 从Active Directory获取计算机名

Active directory 从Active Directory获取计算机名,active-directory,ldap,Active Directory,Ldap,我执行了“LDAP://”查询以获取指定OU内的计算机列表,我的问题是无法仅收集计算机“名称”甚至“cn” 非常感谢您的帮助。如果您可以升级到.NET 3.5,我绝对建议您这样做 使用.NET3.5,您将获得一个新的System.DirectoryServices.AccountManagement名称空间,这使许多工作变得更加简单 要查找所有计算机并枚举它们,您可以执行以下操作: // define a domain context - use your NetBIOS domain name

我执行了“LDAP://”查询以获取指定OU内的计算机列表,我的问题是无法仅收集计算机“名称”甚至“cn”


非常感谢您的帮助。

如果您可以升级到.NET 3.5,我绝对建议您这样做

使用.NET3.5,您将获得一个新的
System.DirectoryServices.AccountManagement
名称空间,这使许多工作变得更加简单

要查找所有计算机并枚举它们,您可以执行以下操作:

// define a domain context - use your NetBIOS domain name
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// set up the principal searcher and give it a "prototype" of what you want to
// search for (Query by Example) - here: a ComputerPrincipal
PrincipalSearcher srch = new PrincipalSearcher();
srch.QueryFilter = new ComputerPrincipal(ctx);;

// do the search
PrincipalSearchResult<Principal> results = srch.FindAll();

// enumerate over results
foreach(ComputerPrincipal cp in results)
{
   string computerName = cp.Name;
}

你需要用
[0]
.Properties[“name”]
集合编制索引,以获取第一个条目(通常也是唯一的条目-几乎没有任何计算机有一个以上的名称)。

在阅读你的文章之前,我刚刚添加了[0],效果很好:)再次感谢Marc。我将不得不对集合(例如属性)进行一些阅读,因为我还不太了解它们是什么,也不知道在处理对象集合时如何解决。我实际上使用的是.Net 3.5。我只是对如何使用.AccountManagement命名空间没有一点模糊的概念,虽然我希望在有更多时间的时候扩展我正在编写的应用程序,但这将是我要看的东西,因为现在最低限度将满足我周一的需要:)@Stephen Murby:一定要读MSDN文章-非常好的东西!
// define a domain context - use your NetBIOS domain name
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// set up the principal searcher and give it a "prototype" of what you want to
// search for (Query by Example) - here: a ComputerPrincipal
PrincipalSearcher srch = new PrincipalSearcher();
srch.QueryFilter = new ComputerPrincipal(ctx);;

// do the search
PrincipalSearchResult<Principal> results = srch.FindAll();

// enumerate over results
foreach(ComputerPrincipal cp in results)
{
   string computerName = cp.Name;
}
pcName = oneMachine.Properties["name"][0].ToString();