0

I'm trying to search AD for all machines in a given OU that have 'TC' in their name, this is what I have so far, but its returning all machines, I need it to return just the machines with 'TC' in their name.

$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}

Not really sure what I should be doing from this point, I am a Powershell Newbie.

4

2 回答 2

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" 
于 2013-10-04T14:44:49.170 回答
0

如果您有可用的AD 模块,则可以使用单个 cmdlet 执行此操作。

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

于 2013-10-04T15:33:04.527 回答