Sometimes it suits me…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
param ( [Parameter(Mandatory=$true)][string]$target, [Parameter(Mandatory=$false)][int]$waitUntilNextCycle=1, [Parameter(Mandatory=$false)][bool]$displayOkStatus=$true ) $ping = New-Object System.Net.NetworkInformation.Ping function Get-Timestamp { return [System.DateTime]::Now.ToString('HH:mm:ss') } if (!$displayOkStatus) { Write-Warning "Only connection errors will be displayed!" } while ($true) { try { $res = $ping.send($target) $status = $res.Status if ($status -ilike "Success") { if ($displayOkStatus) { Write-Host "[Ok $(Get-Timestamp)]" -NoNewline -BackgroundColor DarkGreen -ForegroundColor White } } else { Write-Host "[$status $(Get-Timestamp)]" -NoNewline -BackgroundColor DarkRed -ForegroundColor White } Start-Sleep -Seconds $waitUntilNextCycle } catch { Write-Host "[DNS or network connection error $(Get-Timestamp)]" -NoNewline -BackgroundColor Black -ForegroundColor White } } |