Powershell Script Running Slowly

前端 未结 4 1492
眼角桃花
眼角桃花 2021-01-28 07:46

I\'m writing a script to check the version on about 15 remote servers and the script is taking much longer to execute than I would expect.

$listServers = @(\"com         


        
4条回答
  •  遇见更好的自我
    2021-01-28 08:06

    You don't need to do this in a loop, nor serially. invoke-command takes a collection of ComputerNames, and can run the requests in parallel.

    $listServers = @("compName1", "compName2", "compName3", ... "compName15")
    Invoke-Command -throttlelimit 4 -ComputerName $listServers -Credential $cred -Authentication Kerberos -ScriptBlock {Get-WmiObject -Class Win32_Product -Filter "Name like 'Java [0-9]%'" | Select -ExcludeProperty Version}} -ErrorAction SilentlyContinue -ErrorVariable errorOutput
    

    However, as was pointed out by Tim Ferrell, you can use Get-WMIObject to ping the servers remotely, and if you do it as a job, it will run multiple requests in parallel.

    Get-WMIObject Win32_Product  -Filter "Name like 'Java [0-9]%'" -computername $listServers -throttlelimit 4 -asjob |select -excludeproperty version
    

    Then use the Job cmdlets to receive the results.

提交回复
热议问题