Powershell脚本查找计算机速度慢



我有一个脚本,它应该在AD中搜索具有相同名称的不同ou中的计算机。如:

Get-ADComputer -filter * -Searchbase "OU=domain,DC=home,DC=com"  -properties * |
Where-Object {$_.DistinguishedName -like "*XXX09*"} |
Select name, DistinguishedName

一切工作正常,但它是非常慢,有什么方法来加快它,或构建不同的脚本?

您不仅可以通过使用过滤器来加快此速度,而且,使用-Properties *要求ALL属性。在本例中,这是无用且耗时的,因为您只想检索Name和DistinguishedName。

Get-ADCumputer默认已返回以下属性:
DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName.

Get-ADComputer -Filter "DistinguishedName -like '*XXX09*'" | Select-Object Name, DistinguishedName

在搜索过程中使用过滤器,而不是在搜索后使用过滤器,将大大减少查询时间。

Get-ADComputer -filter 'DistinguishedName -like "*XXX09*"' -Searchbase "OU=domain,DC=home,DC=com" -properties * | select name, DistinguishedName

您可能需要稍微调整查询,但我用'name'而不是' distincishedname '进行了测试,并且工作得很好(并且快得多;))

最新更新