Last week I received very nice task from my boss. To check if it’s possible to receive some data from our primary SCCM servers around the world. I created a script and then wanted to ran it. As it was pretty important task he wanted to know results “on the fly”. I exported values from every server to separate CSV file and store it to our share. Unfortunately on the list were also servers from not-so-well-connected locations (e.g. some sites in Africa). I decided to start from fastest connection to slowest.

Note: We don’t have PowerShell remoting enabled on the servers so it was not possible to use Invoke-Command – just for clarification.

I decided to ping servers on my list (loaded to TXT file) and then sort it based on response time. You can see the idea here:

PS C:\> Test-Connection FarComputer, MyComputer, AnyComputer -Count 1 | ft Address, ResponseTime -AutoSize
Address        ResponseTime
–––––––        ––––––––––––
MyComputer     0
AnyComputer    2
FarComputer    211

You catch it, right? Now I just need to sort it via ResponseTime and pass it down the pipeline. In my final solution I didn’t use just one ping, I used standard four so I added hashtable to store the sum of ResponseTimes per server and then sorted this hashtable.

My final solution then was:

PS C:\> $h=@{}
PS C:\> Get-Content .\servers.txt |% { Test-Connection $_ } |%
>> { $h[$_.Address] = $h[$_.Address] += $_.ResponseTime }
PS C:\> $h.GetEnumerator() | Sort Value | Select Name | MyFunctionHere $_

ResponseTimes are stored in hashtable (see the second line of code) and then sorted by value. That sorted list is then used as input to my function which processes all servers one by one and saves the data as requested.

Note: It “can’t” happen but if it’s not able to ping some server, that one will not be included in hashtable.