The *watch-ping.ps1* Script
===========================

This PowerShell script pings the given host continously and shows the roundtrip times in a horizontal chart.

Parameters
----------
```powershell
/Repos/PowerShell/scripts/watch-ping.ps1 [[-hostname] <String>] [[-timeInterval] <Int32>] [<CommonParameters>]

-hostname <String>
    Specifies the hostname or IP address to ping (windows.com by default)
    
    Required?                    false
    Position?                    1
    Default value                windows.com
    Accept pipeline input?       false
    Accept wildcard characters?  false

-timeInterval <Int32>
    Specifies the time interval in milliseconds between two pings (1000 by default)
    
    Required?                    false
    Position?                    2
    Default value                1000
    Accept pipeline input?       false
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```

Example
-------
```powershell
PS> ./watch-ping.ps1



PING ROUNDTRIP TIMES TO: windows.com
#1 ██████████████ 136ms
#2 ████████████████ 154ms
#3 █████████████████████████ 234ms
...

```

Notes
-----
Author: Markus Fleschutz | License: CC0

Related Links
-------------
https://github.com/fleschutz/PowerShell

Script Content
--------------
```powershell
<#
.SYNOPSIS
	Watch pinging a host
.DESCRIPTION
	This PowerShell script pings the given host continously and shows the roundtrip times in a horizontal chart.
.PARAMETER hostname
	Specifies the hostname or IP address to ping (windows.com by default)
.PARAMETER timeInterval
	Specifies the time interval in milliseconds between two pings (1000 by default)
.EXAMPLE
	PS> ./watch-ping.ps1

	   PING ROUNDTRIP TIMES TO: windows.com
	#1 ██████████████ 136ms
	#2 ████████████████ 154ms
	#3 █████████████████████████ 234ms
	...
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$hostname = "windows.com", [int]$timeInterval = 1000)

function GetPingLatency([string]$hostname) {
	$hostsArray = $hostname.Split(",")
	$tasks = $hostsArray | foreach {
		(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000)
	}
	[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)
	$num = ($value * 108.0) / $maxValue
	while ($num -ge 1.0) {
		Write-Host -noNewLine "█"
		$num -= 1.0
	}
	if ($num -ge 0.875) {
		Write-Host -noNewLine "▉"
	} elseif ($num -ge 0.75) {
		Write-Host -noNewLine "▊"
	} elseif ($num -ge 0.625) {
		Write-Host -noNewLine "▋"
	} elseif ($num -ge 0.5) {
		Write-Host -noNewLine "▌"
	} elseif ($num -ge 0.375) {
		Write-Host -noNewLine "▍"
	} elseif ($num -ge 0.25) {
		Write-Host -noNewLine "▎"
	} elseif ($num -ge 0.125) {
		Write-Host -noNewLine "▏"
	}
	Write-Host " $text"
}

try {
	Write-Host "`n   PING ROUNDTRIP TIMES TO: $hostname" -foregroundColor green
	[int]$count = 1
	do {
		Write-Host "#$count " -noNewline
		[float]$latency = GetPingLatency $hostname
		WriteChartLine $latency 1000.0 "$($latency)ms"
		Start-Sleep -Milliseconds $timeInterval
		$count++
	} while($true)
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}
```

*(page generated by convert-ps2md.ps1 as of 01/23/2025 12:15:25)*
