Update ping-remote-hosts.ps1

This commit is contained in:
Markus Fleschutz 2024-05-07 10:01:06 +02:00
parent f74ab0c771
commit 902f19c64a

View File

@ -7,7 +7,7 @@
Specifies the hosts to ping, seperated by commata (10 Internet servers by default) Specifies the hosts to ping, seperated by commata (10 Internet servers by default)
.EXAMPLE .EXAMPLE
PS> ./ping-remote-hosts.ps1 PS> ./ping-remote-hosts.ps1
Online with 18ms latency (13ms...109ms, 0/10 ping loss) Online with 18ms latency (13...109ms, 0/10 ping loss)
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -18,14 +18,12 @@ param([string]$hosts = "bing.com,cnn.com,dropbox.com,github.com,google.com,ibm.c
try { try {
$hostsArray = $hosts.Split(",") $hostsArray = $hosts.Split(",")
$parallelTasks = $hostsArray | foreach { $tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,750) }
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,750)
}
[int]$min = 9999999 [int]$min = 9999999
[int]$max = [int]$avg = [int]$success = 0 [int]$max = [int]$avg = [int]$success = 0
[int]$total = $hostsArray.Count [int]$total = $hostsArray.Count
[Threading.Tasks.Task]::WaitAll($parallelTasks) [Threading.Tasks.Task]::WaitAll($tasks)
foreach($ping in $parallelTasks.Result) { foreach($ping in $tasks.Result) {
if ($ping.Status -ne "Success") { continue } if ($ping.Status -ne "Success") { continue }
$success++ $success++
[int]$latency = $ping.RoundtripTime [int]$latency = $ping.RoundtripTime
@ -36,7 +34,7 @@ try {
[int]$loss = $total - $success [int]$loss = $total - $success
if ($success -ne 0) { if ($success -ne 0) {
$avg /= $success $avg /= $success
Write-Host "✅ Online with $($avg)ms latency ($($min)ms...$($max)ms, $loss/$total ping loss)" Write-Host "✅ Online with $($avg)ms latency ($($min)...$($max)ms, $loss/$total ping loss)"
} else { } else {
Write-Host "⚠️ Offline ($loss/$total ping loss)" Write-Host "⚠️ Offline ($loss/$total ping loss)"
} }