drew.d.lenhart

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

Monitor Your Servers with Powershell & More – Part 2

2015/06/30

Last post, I visited some PowerShell magic. Catch up with the last post. Next I'm going to introduce a quick AutoIT script to execute the PowerShell script and automate using Windows Task Scheduler.

If your not familiar with AutoIT, check out a previous post of mine. It's not the best tutorial to get you into AutoIT, but its a decent start.


AutoIT

This code is simple enough, invoke the Run() function which starts PowerShell.exe via the command line with some parameters. Notice the SW_HIDE. I love how AutoIT can hide your window and make the process run in the background. Sure you could probably do this with a batch script, but hey, I like every chance to use AutoIT! Open up the script up_down.au3:

;Run PS UP/Down script * hide window with SW_HIDE
;Drew Lenhart
#include <Constants.au3>

;path
$I = "C:stUPdownupdown_v2.ps1"

;execute & hide window
$rc = Run("PowerShell.exe -ExecutionPolicy Bypass -File " & $I, "", @SW_HIDE)

If $rc = 1 Then
   MsgBox($mb_systemmodal, "ERROR", "Something happened with the script!", 10)
EndIf

Now to make it easier, compile the code by selecting Tools >> Compile, or right click on the source file and choose Compile Script. You should now have an executable; up_down.exe!


Automation!

Open Windows Task Scheduler by opening the start menu and search "Task Scheduler". On the right, click "Create Task".

ts2

Fill out the options in the General tab. I like to check Run whether user is logged in or not. This is incase Windows updates get applied and reboots when I'm not around, the script will continue to run as scheduled.

ts3

Click the Trigger tab next, this is the scheduling portion. In this example, I'm setting it to run every 10 minutes. This is your choice, run every 10 minutes, 20 minutes, hours, etc.

ts4

Lastly click Actions tab. In the New Action, browse for the up_down.exe executable that was compiled earlier!

ts5

You will now see your newly created task listed in the Task Scheduler Library. If for any reason you need to temporarily disable/delete this task, there are options in the right task panel.

ts6

You now have successfully set up a simple UP/DOWN server monitor!


Thanks for reading! --Drew

Download Part 2 Code - 6/29/2015