查询AD以查找OU中名称中包含TC的所有计算机

时间:2013-10-04 14:21:46

标签: windows powershell active-directory

我正在尝试在AD中搜索给定OU中名称中包含“TC”的所有计算机,这是我到目前为止所拥有的,但它返回所有计算机,我需要它返回只有机器' TC'在他们的名下。

$root = ([adsi]'LDAP://OU=PCs,OU=Student Computers,DC=student,DC=belfastmet,DC=int','objectCategory=computer')
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(objectCategory=computer)"
$searcher.pageSize=1000
$searcher.propertiesToLoad.Add("name")
$computers = $searcher.findall()
$computers | foreach {$_.properties.name}

从这一点开始,我不确定自己应该做些什么,我是Powershell Newbie。

2 个答案:

答案 0 :(得分:0)

您有两种选择。您可以获取所有计算机,然后使用Powershell cmdlet进行过滤,或者您的ldap过滤器反映您想要的(更好)。试试这个:

$searcher.filter = "(&(objectCategory=computer)(cn=TN*))"

使用ActiveDirectoryModule,您可以使用过滤器在特定OU中找到计算机,并使用SearchBase将搜索限制为所需的OU(假设下面示例中为YourDomain.com \ YourOU):

$adcomputers = Get-ADComputer -Filter {name -like "TC*"} -Searchbase "OU=YourOU,DC=YourDomain,DC=com" 

答案 1 :(得分:0)

如果您有[{3}}可用,则可以使用一个cmdlet执行此操作。

get-adcomputer -filter {name -like "*TC*"}