Wednesday, May 28, 2014

Monitoring Invoke-Command on Remote Servers

So, I've been playing around with Invoke-Command to search event logs on remote servers.

$j = Invoke-Command -ComputerName $servers -ScriptBlock $ScriptBlock -ArgumentList $NumberOfHours -AsJob
Do {
 $jobcount = $j.childjobs.count
 $index = ($j.childjobs | ?{$_.state -eq "completed"}).count
 Write-Progress -Activity "reading remote logs" -status $j.state -PercentComplete (($index / $jobcount)*100)
} while ($j.state -ne "completed")

By adding the -AsJob switch to the Invoke-Command, the variable $J contains the job results. It appears to dynamically update as the job progresses along, detailing the servers that have completed and those that have not. Now I am able to create  a status bar that updates based on the % of completed servers.

When the script is complete, I just need to gather the data from the request.

$Results = Receive-Job $J

No comments:

Post a Comment