Update ping-hosts.ps1

This commit is contained in:
Markus Fleschutz 2024-03-24 12:05:31 +01:00
parent 38f047e111
commit 8e9b84491e

View File

@ -10,7 +10,7 @@
.EXAMPLE .EXAMPLE
PS> ./ping-host.ps1 PS> ./ping-host.ps1
Ping roundtrip times of host windows.com: Ping Roundtrip Times to Host: windows.com
136ms 136ms
154ms 154ms
234ms 234ms
@ -25,14 +25,14 @@ param([string]$hostname = "windows.com", [int]$timeInterval = 1000)
function GetPingLatency([string]$hostname) { function GetPingLatency([string]$hostname) {
$hostsArray = $hostname.Split(",") $hostsArray = $hostname.Split(",")
$parallelTasks = $hostsArray | foreach { $tasks = $hostsArray | foreach {
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000) (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 }
} }
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) function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text)
@ -60,7 +60,7 @@ function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text)
} }
try { try {
Write-Output "`nPing roundtrip times of host $($hostname):" Write-Host "`nPing Roundtrip Times to Host: $($hostname)" -foregroundColor green
do { do {
[float]$latency = GetPingLatency $hostname [float]$latency = GetPingLatency $hostname
WriteChartLine $latency 1000.0 "$($latency)ms" WriteChartLine $latency 1000.0 "$($latency)ms"