Abraxas
Abraxas

Reputation: 361

Powershell - Improving Script Execution Time

I've got a script which goes out to computer names from a text file and then polls the services on those machines and writes the results back to a text file.

Here's what I currently have set up:

$Computers = Get-Content computername.txt 

$Output = Foreach ($Computer in $Computers) {
    Write-Output  "`n" "Status of $Computer".ToUpper()
    Get-WMIobject -Computername $Computer win32_service | Where-Object {$_.startname -ne "LocalSystem" -and $_.startname -ne "localservice" -and $_.startname -notlike "*AUTHORITY*"} | ft name, startname, startmode, state 
}

$Output | Out-File ServiceReport.txt

Is there a way to run multiple sessions at once instead of processing this incrementally? IE - send the command to all the systems at once and then receive the response and record appropriately? I've looked in to Invoke-Command and a few other things but have yet to improve time, get results to write back to the file, or get those options working generally.

Upvotes: 0

Views: 58

Answers (1)

mjolinor
mjolinor

Reputation: 68243

Get-WMIObject will multi-thread. You just need to give it more than one computer at a time to work with:

$Computers = Get-Content computername.txt 

Get-WMIobject -Computername $Computers win32_service |
 Where-Object {$_.startname -ne "LocalSystem" -and $_.startname -ne "localservice" -and $_.startname -notlike "*AUTHORITY*"} |
 ft PSComputerName, name, startname, startmode, state |
 Out-string |
 Out-File ServiceReport.txt

You'll lost the ability to have that "Status of $Computer" line before each one but you can make up for it by including the computer name in the selected objects.

Upvotes: 2

Related Questions