Skip to content

Ping-Host

Pings a host

param([string]$hostname = "x.com")

try {
	$remoteHosts = $hostname.Split(",")
	$tasks = $remoteHosts | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,5000) }
	[Threading.Tasks.Task]::WaitAll($tasks)
	foreach($ping in $tasks.Result) {
		if ($ping.Status -eq "Success") {
			Write-Output "? '$hostname' is online with $($ping.RoundtripTime / 2)ms ping to it's IP $($ping.Address)"
			exit 0 # success
		} else {
			Write-Output "?? No reply from '$hostname' (IP $($ping.Address)) - check the connection or maybe the host is down."
			exit 1
		}
	}
	Write-Output "?? No reply from '$hostname' - check the connection or maybe the host is down."
	exit 1
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the hostname or IP address to ping (x.com by default)

An interactive directory of PowerShell scripts.