2014-07-02 38 views
0

如何获取我的域中特定节点的所有子项?如何在我的域中定位特定容器和特定计算机?

$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain 
$objSearcher.SearchScope = "Subtree" 
$objSearcher.PageSize = 1000 
$objSearcher.Filter = "(objectCategory=$strFilter)" 
$colResults = $objSearcher.FindAll() 

上面的代码工作,但给出了整个域。

如果域是contoso.com,我感兴趣的

目标计算机,这将是在这个容器

"LDAP://dc=departmentA,dc=contoso, dc=com" 

此外,如果有内departmentA

+0

我假设你的意思是'OU = departmentA'。作为示例,您提供的LDAP路径指定了一个名为** departmenta.contoso.com **的域,而不是域** contoso.com **中名为** departmentA **的容器。 –

回答

1

非常特定的计算机您的LDAP URI是错误的,如果departmentA是一个组织单位,它应该读取LDAP://OU=departmentA,dc=contoso,dc=com

请你帮个忙,并安装AD PowerShell的模块(RSAT的一部分),那么您的查询就会变成:

Get-ADComputer -Filter $strFilter -SearchBase "ou=departmentA,dc=contoso,dc=com" 
0

A“搜索根”在LDAP术语是根节点到搜索适用。不带参数的New-Object System.DirectoryServices.DirectoryEntry返回当前会话的登录域的根节点。由于您提供域的根节点作为搜索根并将范围设置为“子树”,因此您正在搜索整个域。

如果您提供的LDAP路径作为字符串参数的特定容器,它返回一个表示容器,您可以为SearchRoot属性的值使用的DirectoryEntry对象:

$objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry('LDAP://OU=departmentA,DC=contoso,DC=com') 

我发现在某些情况下,如果仅指定容器的DN的LDAP路径不起作用,则此格式的值为:LDAP://<FQDN>/<Container's DN>,对于此示例,此格式为LDAP://contoso.com/OU=departmentA,DC=contoso,DC=com。但是,我只在向不同的域进行身份验证时遇到此问题。如果您要搜索登录域,则不需要添加FQDN。