Continual Ping of a Computer with an Alert When Successful.

Good day.
Yesterday was a Friday, and it had been a full, but good week of server fun. I was driving home from dinner, and the last thing I wanted to do was stare at my computer screen. Then the on-call page arrived. A server in California was down because of a power outage at the server’s location. While I could do nothing about the power outage, I was responsable for letting our outage management team know when the server was back online, and our server’s shared resources were available.

Well I start the classic DOS command “PING SERVERNAME -T”, and then watched as the time out notices started to scroll up the screen. The power outage was expected to last anywhere from 30 minutes to several hours, and I really didn’t want to watch the computer while the pings kept scrolling on the screen. What I needed was for the ping to continue, and then stop and notify me with an audible alert when the ping was coming back successfully, and the server was back online. This would allow me to enjoy my Friday, and keep an ear tuned to the alert from my PC in the next room.

Some quick Google research got me what I needed, and I was able to create a quick and simple function to make that happen. I started the function, and was able to walk away. Two hours later, I was alerted, and was able to notify our outage management team of the server status, and the fact that the resources were available.

It is now Saturday morning, and I liked the function so much I have polished it up, and enhanced it just a bit. The function name is:
Test-ConnectWithAlert
I have added headers to the function, so to see the syntax of the function type the following:

get-help Test-ConnectWithAlert  -Examples

pp1071_1377176449036

From the screen shot you can see that there are two parameters: -ComputerName and -Voice

  • -ComputerName: Default is LOCALHOST. This is the name of the computer you want to “ping”. Put a computername here
  • -Voice. If used this will give a Text-to-Voice alert instead of an audio tone.  I have a Cepstral voice installed, so the voice is more pleasant than the default MicroSoft voice.

For both examples that I am showing in the following screenshots I am using my LOCALHOST, but there won’t actually be a time out delay.

pp1071_1377176449127

First Example: Test-ConnectWithAlert -ComputerName LOCALHOST

pp1071_1377176449141

Second Example: Test-ConnectWithAlert -ComputerName LOCALHOST -voice

 

Q: So what is actually happening here?  There are three areas of interest.  The first one occurs at line 21.  This Do Loop repeats untill the Test-Connection cmdlet commands  back with a TRUE value.  That occurs when the pinged computer is once again pingable.  Once that condition is met then the script will continue on, and notify the user of the computer’s status.

pp1071_1377533498931

DO Loop: Repeats until the target computer is found to be online.

Also of interest are the two responses to the two methods of notification.  The first method uses a standard WAV file.  I have used chime, but any valid file can be used, including one that you create.  I found the method to play the WAV file here: playing-sounds-in-powershell.html . It is an old post, but a good one, and it details several methods for playing WAV files.

pp1071_1377533498951

WAV Notification: Repeats until the User Responds.

The second notification method uses Text-to-Voice to read a predefined text statement, and convert it to voice. There are some standard Windows voices that you can use, but I have purchased a voice from Cepstral (http://www.cepstral.com/).  I use a lot of voice  notifications for script completions so to me it was worth the slight expense of the voice. I go into more detail on text to voice in other posts, but in the following screen shot you can see generally what is going on .

pp1071_1377533498981

Text-to-Voice Notification Do Loop: Repeats until the User Responds.

 The PS1 file containing the function can be downloaded here:  Test-ConnectionWithAlert.PS1 or you can get it from my Box.Net widget at the bottom of this page. Look for the folder called “Test-ConnectionWithAlert”.  I have commented the various parts of the script with what I think is helpful information, and links to other resources.

Thanks for reading. Let me know if you have any questions or comments.

Have a good day,

Patrick

6 thoughts on “Continual Ping of a Computer with an Alert When Successful.

  1. Awesome, this is exactly what I was looking for so thanks for sharing.
    I did make one addition so that a Dialogue box will pop up and allow you to input the target hostname and pass it directly into the running PS.

    param([string]$ComputerName = “localsystem”,[switch]$voice = $false)
    [System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
    $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter a computer name”, “Computer”, “$env:computername”).toupper()
    do {
    (write-host “Testing connection to $ComputerName.”)
    }
    until (Test-Connection $ComputerName -quiet)#This is where the ping occurs.

    Admittedly I’m not a great script writer, so I’m not 100% if there’s anything redundant or not optimized above, but I know it’s working for me, so I thought I would share back.

    Also so as not to plagiarize, the source of the input box code was:

    windowsitpro(dot)com/blog/getting-input-and-inputboxes-powershell

    Thanks again,
    CL

Leave a reply to Patrick Cancel reply