Hey, are you awake?

Validated01 As many of you know, one of the things I’m doing these days is helping run The Experts Community web site. As such I need to make sure it is up and running. I also like to make sure my blog and a few other sites are responding to HTTP requests. To meet this need I put together a PowerShell function to test a web site and determine if it is running or not.

There are no cmdlets from Microsoft for testing web site connectivity. However, there is a .NET class called System.Net.WebRequest. I put together a short 2.0 function that uses this class and returns a custom object to the pipeline.

The function takes a URL to test and it can be piped. Using a Try/Catch construct the function attempts to get a response from the site. If it fails, then a System.Net.WebException object is caught so I can grab the status code (eg 404 or 500).

The .NET class doesn’t include a property to capture how long the response took, at least not that I can discover. So instead I simply get the time, attempt the request, get the time again and calculate the difference. Close enough for my needs. FInally, the function writes a custom object to the pipeline using New-Object.

When the function is loaded you can run it like this:

PS C:\> test-website http://jdhitsolutions.com/blog

ResponseMS : 253.0145

StatusCode : 200

Status     : OK

URL        : http://jdhitsolutions.com/blog

DateTime   : 4/7/2010 11:04:30 AM

With this in place, I then turned to the notification process. Windows PowerShell 2.0 includes a cmdlet, Send-MailMessage, which can deliver an SMTP message. My script runs through a list of URLs, saves the objects to a variable, which I then parse out to build a message string. Each URL has its own message which is temporary stored in an array. Then the array is converted to a string and used as the message body for my email notification.

#build message string for email           
$data | foreach {           
    $m=("{0} {1} {2} ({3}) {4} milliseconds" -f `

Depending on your SMTP server configuration you may or may not need to specify a credential. Your SMTP server may also need some minor reconfiguration depending on your network.

The last step was to simply create a scheduled task to run my PowerShell script:

powershell.exe -noprofile -noninteractive -file c:\scripts\ScheduledWebCheck.ps1

Now I get an hourly report that lets me know the web site status, which thankfully is almost always up.

One thought on “Hey, are you awake?

  1. Jeff see:


    Tis might be of interest for a future blog as your above bolg solves a couple of problems but does not discover the rediredcted URL. I looked for a quick way in VBS but didn’t find one. Haben’t looked at PowerShell for a solution.

    Also the following might be interesting for your IIS Admin blogs. It’s AD/PowerShell/IIS related.



Comments are closed.