# encoding: ascii # api: powershell # title: ping check using dotNet # description: ping check using dotNet ping. pieced together from existing examples on the web. Had to use $erroractionpreference = “SilentlyContinue” to make it work on non-existing systems. Added to DNS check to prevent a few more errors. This is part of a script that queries an internal db and for every record does a ping to verify its online state # version: 0.1 # type: function # author: jkavanagh58 # license: CC0 # x-poshcode-id: 1420 # x-archived: 2009-10-27T22:36:03 # # function dnsref ($computername) { $ErrorActionPreference = "SilentlyContinue" $testrun=$Null trap { Write-Host "ERROR: $computername does not exist in DNS" -fore Yellow Throw $_ } $testrun=[net.dns]::GetHostByName($computername) if ($testrun -eq $Null){ Write-Host "No DNS Record" } else { foreach ($alias in $testrun){ PingX($alias.addresslist) } } } function PingX($ip) { $ErrorActionPreference="SilentlyContinue" $ping = New-Object System.Net.NetworkInformation.ping #trap {$_.Exception.Message ;$pingres = $_Exception.Message; continue} if ($_Exception.Message -eq $null) { $pingres = ($ping.send($ip)).Status | Out-Null Write-Host $computername - $ip is REACHABLE -background "GREEN" -foreground "BLACk"} else {Write-Host $computername - $ip is NOT reachable -background "RED" -foreground "BLACk"} return $pingres }