Developing A HTTP Script Monitor In PowerShell

Adam Bertram

Adam Bertram

Read more posts by this author.

How would you define a web server as being “up”? It doesn’t just respond to a simple Internet Control Message Protocol (ICMP) echo request (ping). Even though this is a great indicator of a web server’s operating system being online – and it being connected to the network – it doesn’t say much for the actual service the web server is providing. That’s where a PowerShell script comes in.

When monitoring systems, it’s important to track as close to the real service as possible. In a web server’s case that service is an HTTP request. A web request consists of lots of interdependent parts, including things like the server being connected to the network, having a MAC address, an IP address, a TCP port that’s listening on port 80 and, at the top of this stack, the HTTP status code that the server returns. It’s important to monitor as close to the service as possible, so let’s focus on the HTTP status code.

Command Line Script

There are numerous ways to monitor the status code returned by a web server. We’ll focus on rolling command line script, which is an excellent way to incrementally build upon certain functionality. To create these scripts, I always use PowerShell. It’s a great scripting language and the de facto standard in the Windows world.

Because PowerShell doesn’t natively have a command that can get the HTTP status code from a web server, we’ll need to take a standard approach and use a .NET object. And since a PowerShell script is ultimately built on top of the .NET framework, this is a trivial task. To start off, we’ll need to use the System.Net.WebRequest object. In particular, you’ll need to call a static method called Create and pass the URL to this to build the request.

$req = [system.Net.WebRequest]::Create('')

Making the Request

You can see above that I’m creating a web request for If you look at the request, you’ll see many common properties that will be used to make the request.

But the code doesn’t have much yet; I still need to make the actual request to Google’s web server and get the response. To do this, I’ll need to use the GetResponse() method. This makes the request and returns the response from the web server.

$res = $req.GetResponse()

You can see now that all of the information returned. Notice that the $res object has a StatusCode property – this is what we’re looking for. At this point, we can either leave this as is and use this code in a larger script, or individually pull out the StatusCode by referring to it in dot notation.


Assigning an Integer

This will return “OK,” but there’s no such thing as an “OK” HTTP status code. They’re unique integers. To get the actual HTTP status code, we’ll need to cast this property to an integer. This will return the actual HTTP status code.


Notice now we can get the real HTTP status code, which is 200.

PowerShell is a powerful scripting language that does a lot more than most sysadmins are aware of. And even if PowerShell doesn’t natively support a task, chances are it can still be done through native .NET objects.

Once you begin to create more code like this, you’ll inevitably find that this kind of code can be treated as building blocks. Continue learning small snippets of code like this, and you’ll have a large bag of script tricks that can be applied in numerous situations.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!