Learning PowerShell – Lesson Thirteen

Share:
PowerShell

PowerShell Lesson Thirteen

By: Steven Aiello

Teach Your Self PowerShell – Setting Our Ground Work for a Free Monitoring System

Note: To interact with running processes and services from PowerShell you will have to run Windows PowerShell ISE as an administrator!

Note: I have discovered Amazon will limit the amount of SMTP messages that you can send without a special request. It is good for the purpose of the lab but again not a production solution

In our last lesson we learned how to install the SMTP server via PowerShell. We did have to use the GUI to configure the server; however, with Windows Server 2012 many more options are configurable with PowerShell. In this lesson we will take the framework we built a few lessons ago and build upon that so that we can monitor services and alert us if they aren’t running.

What we are going to do this lesson is take everything that we’ve learned any generate a fully functioning script that e-mails us with:

The name of the server

The name of the service that is no longer running

A timer variable which we can adjust to change the frequency of the service monitor

Break long lines of code onto smaller separate lines

 

How we are going to go about this is take the script that we had created in lesson eleven and for this final lesson I will give you the end result of the script. What we will do is compare step by step what we changed to make this a more useful script using the functions we had covered in this course.

 

Let’s start off by looking at the original script from lesson 11:

 

 

This should look very familiar to you. The first things we will review are the variables in the script

 

When we ran the script interactively from the command line of from the PowerShell ISE, there were some things we didn’t have to account for because we were running the script interactively. Compare the above script variables with the ones below which we will use in our finished product.

As you can see here we have a few additional variables that allow us to accomplish our tasks

 

The first addition you may notice is:

$RUN = “true”

We set this variable because we need to create a loop which will continue to run out monitoring script. The script will run as long as the condition of the $RUN variable equals true.

 

The second addition is:

$TIMER = 300

This is the interval value that we will use for our script. The script will check the services every 300 seconds or 5 minutes. 5 minutes is a fairly reasonable value of frequency to check the services on the system.

 

The third variable is:

$SMTP_SERVER =

This will be the IP address of your SMTP server. See the note about Amazon’s policies at the beginning of this lesson about SMTP relaying*

 

The fourth and fifth variables are:

$ADMIN_EMAIL$ALERT_EMAIL

These are the sender and receiver email addresses that we will use for our script. You could name these something that’s easier for you to remember like $SOURCE_EMAIL and $DESTINATION_EMAIL if those variables make it easier for you to remember.

Lastly we have the following two variables:

$EMAIL_BODY$EMAIL_SUBJECT

These two are aptly named, and are fairly self-explanatory.

Once our variables are defined we enter into the body of our script. We see that we have started with a wile loop (which really could be any type of loop that will run until a condition is met). We are using our $RUN variable which is set to true and will never be set to false. We can see this on line 17 of the script.

 

The next line contains the familiar foreach statement that we’ve been using throughout the lessons. It’s collecting all the data from our CSV file which we defined up in our variables section. The foreach will loop through all the items in the collection and assign the current object in the collection of $SERVICES to the $SERVICE variable. From there we can access all the parameters in the current $SERVICE object.

Line 20 is exactly the same as the previous script so there shouldn’t be any mystery of what’s going on there. Where just getting the service name that we want to monitor. Once we have the service name that we want to monitor on line 22 and line 25 we start constructing the subject and body of the e-mail message that we will send if the service has stopped.

We see 2 things here:

We see that we can use the “+” sign to concatenate strings and variable together.

We see that if we have very long lines in our scripts we can move a portion of the line to the next line by using the single quote “ ’ ”.

Next on line 27 comes the real meat of our code which really isn’t anything but and if else statement.

On line 28 we check to make sure that the $SERVICE_TO_CHECK’s running status is set to what or DesiredStatus is. If those to statuses are the same we do nothing because the services is running as desired; however, if these conditions to note match we invoke our Send-MailMessage command to alert us of the fact that the service is down.

Finally you will see that we use the Start-Sleep command with the $TIMER variable that we set on line 37. This makes the script wait another 5 minutes before it checks the status of the services again.

As you can see there’s a lot of power in this little script, and because we’re reading the data from the CSV file we can expand the usefulness of the script almost any time to add more services.

Now all you have to do is add this script to you Windows task schedule, and have it start on boot. This way every time your server boots it will start monitoring you of your critical services! Of course there are many monitoring systems out there; however, if you only have a handful of servers and services you need to monitor this is a very viable solution.

 

 

 

 

 

Share:

Leave a reply

nine + one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.