简体   繁体   中英

Powershell script is slow to find computers

I have a script that is supposed to search for computers in different OUs with the same name in AD. eg.

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

Everything works fine, but it is terribly slow, is there any way to speed it up, or build the script differently?

Not only can you speed-up this by using a filter, but also, using -Properties * is asking for ALL properties. That is useless and time consuming in this case because you only want to retrieve the Name and DistinguishedName.

Get-ADCumputer by default already returns these properties:
DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName .

Try

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

Use the filter during the search instead of after will reduce the query time quite a bit.

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

You might need to tune the query slighty, but i tested it with 'name' instead of 'DistinguishedName' and that works just fine (and quite a bit quicker;))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM