Web Testing with PowerShell

I run a self-hosted WordPress blog here as part of a hosted package.  I run this on a very tight budget so I’m pretty sure I share resources with other tenants. This means that sometimes the server is unavailable, usually for only a brief period of time. I have the JetPack WordPress plugin configured to monitor when the site is up or down. But I thought I’d add another layer of testing using PowerShell.

Fellow MVP Chrissy LeMaire sent me a message on Twitter that my blog was down with a link to the web site http://downforeveryoneorjustme.com. What I liked about the site is that it produces a very simple page. Once I saw that I realized I could use Invoke-Webrequest and build a simple PowerShell function to scrape the page results.

It is very easy to get the page.

The resulting object will include the parsedHTML as a property. I knew from looking at the page source in my browser that the information I wanted was in a DIV tag with an ID property called “container”.

image

With this information I can use the getElementbyID() method to get this item.

Fortunately there is only a single result for this ID. If there had been several I would have had to include additional filtering code. All I need to do is look at the InnerText property.

image

I can refine this by getting just the first line.

image

I now have a few commands that work. Time to build a function around them so I have an easy, re-usable tool. I have put the script file as a gist on GitHub.

The script also includes an alias. I also added a parameter to return the tested date time in GMT should you need that.

Now, it is very easy to test if a site is up or down.

image

I’m not sure what sort of monitoring and reporting tool I’ll build with this, but hopefully it will be worth blogging about. In the mean time, give this a shot and let me know what you think. If you run into problems with the code, please post something in the discussion section on the Gist page.