From 8e9b84491ebf694309cd068e7c3e23081fc067f2 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Sun, 24 Mar 2024 12:05:31 +0100 Subject: [PATCH] Update ping-hosts.ps1 --- scripts/ping-host.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/ping-host.ps1 b/scripts/ping-host.ps1 index 392b1372..85c6f353 100644 --- a/scripts/ping-host.ps1 +++ b/scripts/ping-host.ps1 @@ -10,7 +10,7 @@ .EXAMPLE PS> ./ping-host.ps1 - Ping roundtrip times of host windows.com: + Ping Roundtrip Times to Host: windows.com ██████████████ 136ms ████████████████ 154ms █████████████████████████ 234ms @@ -25,14 +25,14 @@ param([string]$hostname = "windows.com", [int]$timeInterval = 1000) function GetPingLatency([string]$hostname) { $hostsArray = $hostname.Split(",") - $parallelTasks = $hostsArray | foreach { - (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000) - } - [Threading.Tasks.Task]::WaitAll($parallelTasks) - foreach($ping in $parallelTasks.Result) { - if ($ping.Status -eq "Success") { return $ping.RoundtripTime } + $tasks = $hostsArray | foreach { + (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000) } - return 1000.0 + [Threading.Tasks.Task]::WaitAll($tasks) + foreach($ping in $tasks.Result) { + if ($ping.Status -eq "Success") { return $ping.RoundtripTime } + } + return 1000 } function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text) @@ -60,7 +60,7 @@ function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text) } try { - Write-Output "`nPing roundtrip times of host $($hostname):" + Write-Host "`nPing Roundtrip Times to Host: $($hostname)" -foregroundColor green do { [float]$latency = GetPingLatency $hostname WriteChartLine $latency 1000.0 "$($latency)ms"