Comparing Computer Data Received from PowerShell Remoting

by Oct 27, 2017

PowerShell remoting is a very fast way to query multiple computers because PowerShell remoting works in parallel. Here is a real world use case that illustrates a couple of interesting techniques.

The goal is to retrieve the list of running processes from two computers, then find the differences.

For maximum speed, the process lists are queried via PowerShell remoting and Invoke-Command and results are received from both computers.

To separate the incoming data, Group-Object is used. It groups the data sets by computer name. The result is a hash table, and the computer names are the keys in this hash table.

Next, Compare-Object is used to quickly compare both lists and find the differences:

# get data in parallel via PowerShell remoting 
# make sure you adjust the computer names
$computer1 = 'server1'
$computer2 = 'server2'
$data = Invoke-Command { Get-Process } -ComputerName $computer1, $computer2

# separate the data per computer
$infos = $data | Group-Object -Property PSComputerName -AsHashTable -AsString

# find differences in running processes
Compare-Object -ReferenceObject $infos.$computer1 -DifferenceObject $infos.$computer2 -Property ProcessName -PassThru |
  Sort-Object -Property ProcessName |
  Select-Object -Property ProcessName, Id, PSComputerName, SideIndicator

Twitter This Tip! ReTweet this Tip!