---
title: Build Your Own HTTP Monitor with PowerShell
description: Ensure your web service's uptime with a custom HTTP monitor. A beginner-friendly guide to coding in PowerShell.
canonical: https://adamtheautomator.com/http-monitor/
---

# Build Your Own HTTP Monitor with PowerShell

> Ensure your web service's uptime with a custom HTTP monitor. A beginner-friendly guide to coding in PowerShell.

Source: https://adamtheautomator.com/http-monitor/

---

- Build Your Own HTTP Monitor with PowerShell Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# Build Your Own HTTP Monitor with PowerShell

        Published:1 July 2019 - 2 min. read

- PowerShell

          [](https://adamtheautomator.com/author/adam-bertram/)

            [Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)

            Read [more tutorials](https://adamtheautomator.com/author/adam-bertram/) by Adam Bertram!

-

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

Table of Contents

- Building the HTTP Monitor
- Making the Request
- Assigning an Integer

                XFacebookLinkedIn

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) but more importantly, an HTTP monitor is showing as down.  That's where a PowerShell script comes in.

When [monitoring systems](https://www.ipswitch.com/application-and-network-monitoring), 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.

## Building the HTTP Monitor

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](https://www.whatsupgold.com/blog), 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('http://www.google.com')

You also use PowerShell's Invoke-WebRequest for this task. Check out the [in-depth post here](https://adamtheautomator.com/invoke-webrequest/) on the ATA blog for more info.

## Making the Request

You can see above that I'm creating a web request for google.com. 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. It's not actually an HTTP monitor just 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.

$res.StatusCode

## 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 for the HTTP monitor script, we'll need to cast this property to an integer. This will return the actual HTTP status code.

[int]$res.StatusCode

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.

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
