Learning PowerShell – Lesson Twelve
PowerShell Lesson Twelve
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!
In this lesson we will accomplish two things:
First we will learn how to install a SMTP (out bound e-mail server) with PowerShell. It will introduce you to installing Windows feature from the command line rather than the Windows GUI. Second we will use this feature to send ourselves an e-mail messages if a service on a system that we care about stops or crashes.
Note: while we will install the SMTP-Server we will still have to use the GUI to configure some of its options.
This will be the project that we will work on for our last four lessons. We will construct a simple free service monitoring system that we can use in our environment to alert us if a service we want to monitor stops. This may not be the fanciest way to monitor our environment but if you only have a few servers that you need to keep track of it may be just the tool to do the job.
For the next portion of the lesson you will need access to a Windows Server Machine. There are several ways that you can gain access to this technology.
Download VMPlayer and get a trial copy of Windows Server 2008 R2 from Microsofthttp://www.vmware.com/products/player/
Use Amazon EC2 where you can gain access to a Windows Server for a reasonable price
If you or a friend has some old hardware you can install a trial version of Windows Server as it will run on almost any hardware.
Finally if you are a student you can visit Microsoft’s website where they give students educational access to almost all of their software for free. It’s a great resource if you’re trying to learn! https://www.dreamspark.com/
Let’s move on to installing an SMTP mail server on Windows Server 2008 R2!
First we will open up PowerShell and execute the command:
We will then execute the command which will allow us to see all of the Windows Features that are available to install:
PowerShell will collect the data for your system and present you with a menu of options
As you can see this menu will present you will many options
Can you spot in the image the one that we will install?
We will next execute the command to install our select Windows Service:
As you can see PowerShell will start the installation of the SMTP-Server for you
As you can see after the service is finished installing you will get a status report of what occurred and any additional information that is relevant to the install.
As you can see:
Success = True
Restart Needed = No
Exit Code = Success
Feature Result = SMTP Server Tools, SMTP Server
As you can see if you look in the Administrative Tools menu there will now be an Internet Information Services (IIS) 6.0 Manager
We will need to select this option to do the final configuration of the SMTP server. Sadly even though Windows Server 2008 R2 was a large step forward, it’s not as fully functional when it comes to server configuration as Windows Server 2012.
We are going to open the new menu option we have and right click the SMTP server and select properties:
From there we will select the access tab on the menu and select the “Relay” button
We will then select the below options to allow all systems to use our SMTP server
Note: This is NOT how you configure a production server! This is for lab purposes only. A SMTP relay left in this condition can be used for SPAM relay and could get your IP address space black listed. Again this is ONLY for lab configuration.
Select “OK” and you will now have a SMTP relay server that will accept e-mail from any one. Again this should only be used for the purpose of the lab and not left in this condition if your SMTP server is connected to the Internet in any way.
To test this SMTP server you can execute the following command:
If everything was done correctly you should receive an e-mail from your PowerShell script! If you don’t get your e-mail be sure to check your spam filter it maybe there.
So now we have all the pieces we need to finish our PowerShell script to alert us of when a service of interest stops functioning.