This is a nicer looking (than) Ping function I wrote in Powershell a while ago.
It’s a simple Ping function that only writes a new line when there is a state change. So the output is much neater then a ping -t command that rapidly scrolls off the screen.
The function will take the following parameters
JPing [-Target] <string> [-Count <int>] [-StateChanes <int>] [TimeInt <int>] [LogFile <string>]
- Target (mandatory) the IP or FQDN of the target
- TimeInt (optional) The delay in seconds between each ping (default 1)
- Count (optional) exit after specific number of pings (default infinite)
- StateChanges (optional) exit after specific number of statechanges (default infinite)
- LogFile (optional) path to log file
Any switches can be used. If the LogFile path cannot be written to, the function displays an error, disable file logging and continue.
function JPing {
Param (
[Parameter(Mandatory=$true)] [string]$Target,
[Parameter(Mandatory=$false, ValueFromPipeline=$False)] [int]$TimeInt = 1, # time between pings in seconds (note a failed ping has to time out so the int will be longer for connection failures)
[Parameter(Mandatory=$false, ValueFromPipeline=$False)] [int]$Count = 0, # number of pings before scipt ends. 0 = infinate
[Parameter(Mandatory=$false, ValueFromPipeline=$False)] [int]$StatChanges = -1, # number of state changes before script will end.
[Parameter(Mandatory=$false, ValueFromPipeline=$False)] [string]$LogFile = "DoNotLog"
)
if ($logFile -eq "DoNotLog")
{
# LogFile is not set
$LogToFile = $false
}else{
# logfile set. write a header line and then test the files exists
$LogToFile = $true
$Time = (get-date -f "yyyyMMdd HH:mm:ss").ToString()
# write header line to log file. if path does not exist catch the error and disable file logging.
try
{
"Testing Connection to $Target at $Time" | out-file $LogFile -ErrorAction SilentlyContinue
}
catch
{
$LogToFile = $false
write-host "Error unable to write to $LogFile. Logfile disabled" -ForegroundColor Red
}
}
$FirstRun = $True
$LastState = "Unknown"
$CurrentState = "Unknown"
$NumPings = 0
$NumStatChanges = 0
$origpos = $host.UI.RawUI.CursorPosition
do{
$NumPings ++
# get the current time and ping the target
$Time = (get-date -f "yyyyMMdd HH:mm:ss").ToString()
$bob = test-connection -ComputerName $Target -Count 1 -ErrorAction SilentlyContinue
if ($?)
{
# Ping responds, target is up
$RTime = $bob.ResponseTime
$CurrentState = "Up"
# check if this is the frist ping. if it is then set the LastState to the CurrentState so we dont get a state change and write a header first ping line
if ($FirstRun)
{
$LastState = $CurrentState
write-host "$Time `t $Target is up. response time $RTime ms (first ping) " -ForegroundColor Green
$origpos = $host.UI.RawUI.CursorPosition
# if logging is enabled, write to log file
if ($LogToFile) {"$Time `t $Target is up. response time $RTime ms (first ping)" | out-file $LogFile -Append}
$FirstRun = $false
}
# check if the current state of the target is the same as the last state
if ($CurrentState -eq $LastState)
{
# no state change
$host.UI.RawUI.CursorPosition = $origpos
write-host "$Time `t $Target is up. response time $RTime ms ($NumPings) " -ForegroundColor Green
}else{
# state change. target is now up from down or unknown state
$host.UI.RawUI.CursorPosition = $origpos
write-host "$Time `t $Target is now up. response time $RTime ms ($NumPings) " -ForegroundColor Green
$origpos = $host.UI.RawUI.CursorPosition
# if logging is enabled, write to log file
if ($LogToFile) {"$Time `t $Target is now up. response time $RTime ms ($NumPings)" | out-file $LogFile -Append}
$NumStatChanges ++
}
$LastState = "Up"
}else{
#Ping does not respond. target is down
$CurrentState = "Down"
# check if this is the frist ping. if it is then set the LastState to the CurrentState so we dont get a state change and write a header first ping line
if ($FirstRun)
{
$LastState = $CurrentState
write-host "$Time `t $Target is Down. (first ping) " -ForegroundColor red
$origpos = $host.UI.RawUI.CursorPosition
# if logging is enabled, write to log file
if ($LogToFile) { "$Time `t $Target is Down. (first ping)" | out-file $LogFile -Append}
$FirstRun = $false
}
# check if the current state of the target is the same as the last state
if ($CurrentState -eq $LastState)
{
# no state change
$host.UI.RawUI.CursorPosition = $origpos
write-host "$Time `t $Target is Down. ($NumPings) " -ForegroundColor red
}else{
# state change. target is now down from up or unknown state
$host.UI.RawUI.CursorPosition = $origpos
write-host "$Time `t $Target is now Down. ($NumPings) " -ForegroundColor red
$origpos = $host.UI.RawUI.CursorPosition
# if logging is enabled, write to log file
if ($LogToFile) {"$Time `t $Target is now Down. ($NumPings)" | out-file $LogFile -Append}
$NumStatChanges ++
}
$LastState = "Down"
}
start-sleep -Seconds $TimeInt
$FirstRun = $false
}until ($Count -eq $NumPings -or $StatChanges -eq $NumStatChanges)
}