-
-
Save falkheiland/bfa8e599d1d68e5c52fec82e61be887e to your computer and use it in GitHub Desktop.
A PowerShell Function that uses asynchronous ping commands to quickly ping a fleet of target machines.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Performs a series of asynchronous pings against the target hosts. | |
.PARAMETER HostName | |
A string array of target hosts to ping. | |
.PARAMETER PingCount | |
The number of pings to send against each host. | |
#> | |
function Invoke-FastPing | |
{ | |
[alias('FastPing', 'fping', 'fp')] | |
param | |
( | |
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Computer', 'ComputerName', 'Host')] | |
[String[]] $HostName, | |
[Int] $PingCount = 4 | |
) | |
process | |
{ | |
# Objects to hold items as we process pings | |
$queue = [System.Collections.Queue]::new() | |
$pingHash = @{} | |
# Start an asynchronous ping against each computer | |
foreach ($hn in $HostName) | |
{ | |
if ($pingHash.Keys -notcontains $hn) | |
{ | |
$pingHash.Add($hn, [System.Collections.ArrayList]::new()) | |
} | |
for ($i = 0; $i -lt $PingCount; $i++) | |
{ | |
$ping = [System.Net.Networkinformation.Ping]::new() | |
$object = @{ | |
Host = $hn | |
Ping = $ping | |
Async = $ping.SendPingAsync($hn) | |
} | |
$queue.Enqueue($object) | |
} | |
} | |
# Process the asynchronous pings | |
while ($queue.Count -gt 0) | |
{ | |
$object = $queue.Dequeue() | |
if ($object.Async.IsCompleted -eq $true) | |
{ | |
$null = $pingHash[$object.Host].Add(@{ | |
Host = $object.Host | |
RoundtripTime = $object.Async.Result.RoundtripTime | |
Status = $object.Async.Status | |
}) | |
} | |
else | |
{ | |
$queue.Enqueue($object) | |
} | |
} | |
# Using the ping results in pingHash, calculate the average RoundtripTime | |
foreach ($key in $pingHash.Keys) | |
{ | |
if (($pingHash.$key.Status | Select-Object -Unique) -eq 'RanToCompletion') | |
{ | |
$online = $true | |
} | |
else | |
{ | |
$online = $false | |
} | |
if ($online -eq $true) | |
{ | |
$latency = [System.Collections.ArrayList]::new() | |
foreach ($value in $pingHash.$key) | |
{ | |
if ($value.RoundtripTime) | |
{ | |
$null = $latency.Add($value.RoundtripTime) | |
} | |
} | |
$average = $latency | Measure-Object -Average | |
if ($average.Average) | |
{ | |
$roundtripAverage = [Math]::Round($average.Average, 0) | |
} | |
else | |
{ | |
$roundtripAverage = $null | |
} | |
} | |
else | |
{ | |
$roundtripAverage = $null | |
} | |
[PSCustomObject]@{ | |
ComputerName = $key | |
RoundtripAverage = $roundtripAverage | |
Online = $online | |
} | |
} | |
} # End Process | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment