Simple PowerShell Script for Server Monitoring

How To Create a Simple PowerShell Script for Server Monitoring

Want to keep an eye on how your servers are doing? Here's a guide on using PowerShell to get yourself started in the right direction.

When you need to ensure a server stays online with all of the required services running there are lots of different tools to make this happen both big and small. All of these tools work, but all are built with the generic user in mind. None of these off-the-shelf tools are built with your company’s unique requirements in mind. Building your own server monitoring tool is no simple task and will require effort, but you’ll soon see that by using PowerShell scripts and some determination, you’ll be able to forego spending thousands of dollars on a product and, eventually, have some monitors that work exactly to your specification. You’ll also learn a lot more about PowerShell scripting in the process which is always a good thing!

Before we can start building code, we must first determine how we’re going to build this tool. A monitoring script of any sort has five elements; a trigger, an action, a known good state, a known bad state and optionally an output of some kind. For example, a trigger could be a specific time, the action may be to send an ICMP request to the server, the known good state would be if it responds, the known bad state would be if it does not respond and the output might be sending an email to the administrator notifying them of the threshold. To build a great server monitor of any kind, not just PowerShell, all of these elements must be incorporated into the tool in some way.

To demonstrate how to create simple server monitors in PowerShell, let’s pick a couple of examples. Let’s say I have a web server that needs to be up 24/7. It needs to have a specific Windows service running, and it always needs to always respond on TCP port 80. What are some example elements in play here?

Server Monitor #1

Trigger: Every 5 minutes Action: Get Windows service state Bad State: Not running Good State: Running Output: Send an email to administrator

Server Monitor #2

Trigger: Every 5 minutes Action: Connect to TCP port 80 Bad State: No response Good State: HTTP/200 Output: Send an email to administrator

Each performance monitor can be built into a PowerShell function. I’ll put these functions inside of a script called C:Monitors.ps1.

function Test-WindowsService 

Now that we have two performance monitors built, we can build an invocation script to call these monitors and perform other tasks with them. I’ll create a script called C:CallMonitors.ps1 which will look like this:

## Dot source the functions to make them available
 . C:Monitors.ps1
 ## Define the server(s) we'll be monitoring and the criteria
 $monitors = @(

Notice how everything has a specific structure. All monitors are in an array of hashtables with each attribute necessary. Once the monitor information is in a structure like this, we can easily apply the rules to each by looping over each one in a loop. Schedule a task to call this script every 5 minutes, and you’re golden to monitor your server performance!

The examples provided were just that, examples. Pay attention to the style and how to build the framework; not necessarily the monitoring scripts themselves. By replicating a framework like this, you’ll be able to monitor just about anything in your environment!


Get Started with WhatsUp Gold

Subscribe to our mailing list

Get our latest blog posts delivered in a monthly email.

Loading animation


Comments are disabled in preview mode.