programming, software, technology, anything on my mind...

Monitor Your Servers with Powershell & More - Part 1


Need a quick and dirty method to get a quick UP/DOWN status of your servers? I do! I run several servers for media/storage/development and needed a quick tool to give me a heads up of a downed server.

This project makes use of PowerShell, AutoIT, HTML/CSS/PHP, MySQL & Javascript. The first part of the tutorial I will show you how to create the PowerShell script that essentially does a ping test of a list of servers! The itinerary is as follows:

Part 1 - Create PowerShell Script & E-mail notifications Part 2 - Automate using AutoIT & Windows Task Scheduler

Some needed items for Part 1:

  1. Windows 7/8/8.1 to develop Powershell scripts
  2. Windows 7/8/8.1, Win Server 2008/2012 box to run scripts.
  3. Gmail account for sending e-mail notifications, or any account with an smtp service to use.


Go ahead and download the working code for this part of the tutorial. The code is a tad rough this time, we'll clean things up in the next tutorial. Go ahead and load the files in this location: C:stUpdown, or simply change the file location in the PS script.


  1. updown.ps1 <-- Powershell script
  2. servers.txt <-- List of servers to monitor
  3. Logs <-- Folder - holds updown.htm, used for status and email notification

PowerShell Source

*I'm not going into specifics of PowerShell coding, there are plenty of resources for PS development.

## Desc: Simple Test-Connection Script - pings hosts in servers.txt file
## Auth: Drew D. Lenhart
## http://www.drewlenhart.com
## 05/21/15

##Declare file##

$ServerListFile = "C:stUPdownservers.txt"   
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue  
$LogTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$Outputreport = "" 

##EMAIL function##

function sendMail($datestamp){
    ##Use GMAILs free smtp services, must use port 587
    $smtpServer = "smtp.gmail.com" 
    $srvPort = 587
    $smtpFrom = "from email" 
    $smtpTo = "to email" 
    $messageSubject = "Ping Script Results - $datestamp"
    $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto 
    $message.Subject = $messageSubject 
    $message.IsBodyHTML = $true 
    $message.Body = "<head><pre>$style</pre></head>" 
    $message.Body += Get-Content C:stUPdownLogsupdown.htm 
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer, $srvPort)
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential("gmail address with NO @gmail.com", "gmail password") 
    ##Send message

##Iterate through each hostname in servers.txt#

$failCount = 0

$Outputreport = "<h3>Ping Stats:</h3>"
$Outputreport += "<table><tr style='background-color: grey; color: white'><td>Hostname</td><td>Status</td></tr>"

foreach ($Server in $ServerList) { 
    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {  
        #show some status in console
        echo "$Server is pingable"
        $Outputreport += "<tr><td>$Server</td><td style='background-color: green; color: white'>ONLINE</td></tr>"
    } else { 
        #show some status in console
        echo "$Server seems dead Jim" 
        $Outputreport += "<tr><td>$Server</td><td style='background-color: red'>OFFLINE</td></tr>"
        $failCount = $failCount + 1

$Outputreport += "</table>"
##Save results to .html file for e-mail
$Outputreport | out-file C:stUPdownLogsupdown.htm

##If the counter is greater than 1, send failure email

if ($failCount -ge 1) {
    sendMail "$LogTime"

Feel free to edit the script to your liking. The script is pretty straight forward. Foreach server in server list execute test-Connection, which is essentially the PowerShell way to ping.

Some modifications will need to be made in the sendMail() function. I signed up for an extra Gmail account for this. You will need to make sure the Gmail account has access for less secure apps has been enabled.

**Notice, I am saving the results to the Logs folder as updown.htm, then invoking the sendMail() function which puts the contents of the html file in the email!

Run PS Script

First step is to put some active server names or IP's into the servers.txt file. The server file included in the download code has random IP's. Put a couple of good hostnames or IP's and include a bogus one to test the failure e-mails.

Open updown.ps1 in Windows PowerShell ISE. This IDE acts as the editor and can view console output. Click the play button or F5 to run!


Example of Console Output


Tips for running PS

I ran into two issues trying to run this script. I received a couple errors in the PowerShell ISE pertaining to ACCESS DENIED.

In the console window execute the following:

set-executionpolicy unrestricted

If you are still getting an access denied error, open PowerShell ISE as an Administrator.

Open Logsupdown.htm and you should see something like the following:


OH no! I have a server down! Feel free to edit this HTML markup in the PS script. This was quick and dirty. I chose to only receive e-mails when there is a failure. You could take out the IF statement at the bottom to send all results, leave sendMail "$LogTime" of course.

Hopefully this wasn't too bad of a read for you! Stay tuned for Part 2 as I will walk you through setting up a quick compiled AutoIT script for Windows Task Scheduler to run. Thanks for reading!


Download Part 1 Code (5/26/15)