Update check-ping.ps1

This commit is contained in:
Markus Fleschutz 2023-09-25 21:08:20 +02:00
parent 5be0c8c3c3
commit 8c8bfd7ee7

View File

@ -19,28 +19,26 @@ param([string]$hosts = "bing.com,cnn.com,dropbox.com,github.com,google.com,ibm.c
try {
$hostsArray = $hosts.Split(",")
$parallelTasks = $hostsArray | foreach {
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_, 500)
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,750)
}
[int]$min = 9999999
[int]$max = [int]$avg = [int]$successCount = [int]$lossCount = 0
[int]$totalCount = $hostsArray.Count
[int]$max = [int]$avg = [int]$success = 0
[int]$total = $hostsArray.Count
[Threading.Tasks.Task]::WaitAll($parallelTasks)
foreach($ping in $parallelTasks.Result) {
if ($ping.Status -eq "Success") {
[int]$latency = $ping.RoundtripTime
if ($latency -lt $min) { $min = $latency }
if ($latency -gt $max) { $max = $latency }
$avg += $latency
$successCount++
} else {
$lossCount++
}
if ($ping.Status -ne "Success") { continue }
$success++
[int]$latency = $ping.RoundtripTime
$avg += $latency
if ($latency -lt $min) { $min = $latency }
if ($latency -gt $max) { $max = $latency }
}
if ($successCount -ne 0) {
$avg /= $successCount
Write-Host "✅ Online with $($avg)ms latency average ($($min)ms...$($max)ms, $lossCount/$totalCount ping loss)"
[int]$loss = $total - $success
if ($success -ne 0) {
$avg /= $success
Write-Host "✅ Online with $($avg)ms latency average ($($min)ms...$($max)ms, $loss/$total ping loss)"
} else {
Write-Host "⚠️ Offline ($lossCount/$totalCount ping loss)"
Write-Host "⚠️ Offline ($loss/$total ping loss)"
}
exit 0 # success
} catch {