Posey's Tips & Tricks
DIY Network Monitoring for Hyper-V, Part 3
A PowerShell-based Hyper-V health check can be automated through Task Scheduler to provide a daily status report while also running hourly in silent mode unless a problem is detected.
In the previous post in this series, I showed you what my Hyper-V health check script looks like and I pointed out the various configuration options that exist within the script. Now, I want to wrap things up by showing you how to run the script automatically.
If you think back to my very first article in this series, you will recall that I mentioned wanting to design the script so that it reported the Hyper-V status on a daily basis, but also ran hourly, not displaying anything at all unless there was a problem. The way that I am accomplishing this is through the use of parameters. If you look at the very beginning of the script, you will notice that the script is designed to accept parameters:
param(
[ValidateSet("Daily","Hourly")]
[string]$Mode = "Daily"
)
This block of code accepts either Daily or Hourly as a parameter. The script defaults to Daily if no parameter is supplied.
A little bit further down in the script, I have created this line of code:
$ShowDailyStatus = ($Mode -eq "Daily")
This line of code sets $ShowDailyStatus to either True or False, depending on which parameter has been used.
Later on in the script, I have a line of code that adjusts the scripts behavior based on whether the script is examining the daily status. If $ShowDailyStatus is False and no problems exist, then the GUI is never created, otherwise the GUI is displayed. You can see that command here: If ($OverallProblemsDetected -or $ShowDailyStatus) {
So now that I have talked a bit about parameter use, let’s go ahead and set up the scheduled execution. To get started, open the Task Scheduler and then click on Create Task (don’t use the Create Basic Task option).
When prompted, enter a name and a description for the task that you are creating. This particular task will be for the daily run. Be sure to chooser the option to run only when the user is logged in and to run with the highest privileges. You can see what this looks like in Figure 1.
[Click on image for larger view.]
Figure 1. You will need to run the script with the highest privileges.
Now, go to the Triggers tab and click the New button. Configure the Task Scheduler to run the task according to a schedule. As you can see in Figure 2, I am going to be running the task daily at 9:00 AM. Make sure to select the Enabled button before you move on.
[Click on image for larger view.]
Figure 2. Configure the task to run daily.
Click OK and then select the Actions tab. Click New and then set the Action to Start a Program. Set the Program /Script field to PowerShell.exe. Next, go to the Arguments section and enter:
-NoProfile -ExecutionPolicy Bypass -File "C:\HealthCheck\HealthCheckt.ps1" -Mode Daily
This tells PowerShell not to use a profile and to bypass your script execution policy. It also tells PowerShell which script to run. Notice that at the end of the command, I am setting the mode to Daily.
While you are at it, you will need to be sure that the script is set to start in the correct folder.
Click OK twice to create the scheduled task. If you want to test it, then just right click on the task that you have created and select the Run command from the shortcut menu.
Now, all that’s left is to set up the hourly task. You will do this in exactly the same way, but configure the schedule to run the task hourly. You will also need to set the Mode parameter to Hourly instead of Daily when you specify the command line arguments.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.