Building PowerShell scripts to check a VMM host’s servicing windows

In a recent article, I talked about how System Center Virtual Machine Manager servicing windows could be used to define the acceptable maintenance periods for Hyper-V hosts. As handy as that might be, however, it is possible to build PowerShell scripts that check a host’s servicing window before taking any sort of action. Before I show you how to do this, I need to point out that servicing windows vary in scope. Some occur daily, while others might occur weekly or monthly. Building a PowerShell script that examines every possibility would be a big undertaking and would far exceed the amount of space that I have to work within this article. That being the case, I want to approach this by showing you a few basic techniques that you can apply to your own script. VMM provides a PowerShell cmdlet named Get-SCServicingWindow that you can use to retrieve details about servicing windows. The screen capture below shows what happens when you enter this cmdlet without any parameters.
VMM servicing windows powershell

PowerShell script for VMM servicing window: Key steps

To build a PowerShell script that checks to see if a servicing window is in effect, there are several things that must be done. Some of the key steps include:

  • Check to see which servicing window applies to the host that needs maintenance.
  • Retrieve the data associated with the servicing window.
  • Check the current date and time.
  • Check to see if the servicing window has begun.
  • Check to see if the servicing window has ended.

The good news is that some of these steps can be combined. Suppose for instance that I wanted to perform maintenance on a host named Hyper-V. I could retrieve the servicing window name and the servicing window data in a single step by using this command:

$ServicingWindow = Get-SCServicingWindow -VMHost Hyper-V

This command checks to see which servicing window applies to the host named Hyper-V and then writes the data associated with the servicing window to a variable named $ServicingWindow. You can see what this looks like in the figure below. It is worth noting that a host can have multiple servicing windows, but coping with multiple servicing windows is beyond the scope of this article.

VMM servicing windows powershell

Once you have created a variable containing the servicing window details, you will need to retrieve the current date and time and write them to a pair of variables. PowerShell provides a Get-Date cmdlet that can be used for both, but you will need to perform some simple string manipulation to write the time to a variable. Here are some commands that you can use to get the job done:

$Date = Get-Date
$Time=([String] $Date.Hour + “:” + $Date.Minute)

VMM servicing windows powershell

Variables make it easier

Now that you have retrieved the servicing window data and created variables reflecting the current date and time, it is theoretically possible to begin making some comparisons. However, I think it’s a little bit easier to create a couple of variables to keep track of when the servicing window opens. Once you have created such a variable, then you can make a simple comparison to see if it is currently later than the maintenance window start time. Here is an example:

[DateTime]$StartDate = $ServicingWindow.StartDate
$StartTime = $ServicingWindow.StartTimeOfDay
If ($Date -gt $StartDate) {$Go = $True} else {$Go=$False}
If ($Time -lt $StartTime) {$Go=$False}

In this code block, I started by creating a Boolean variable called $Go. The $Go variable is going to be used to tell PowerShell whether or not it is OK to begin the maintenance cycle. I start by setting this variable to $False, indicating that maintenance should not be performed.

Next, I set up two variables, $StartDate and $StartTime. These variables contain the starting date and time of the servicing window. From there, I compare the current date to the start date and the current time to the start time. If the current date is greater than the start date, then I set the $Go variable to $True, otherwise, it gets set to false.

Next, I would begin making a series of comparisons. In the example code, I only have one comparison set up, however. In this example, I am checking to see if it is currently earlier than the designated start time. If so, then I am setting $Go to $False. It is important to note that this and any subsequent comparisons should only check to see if a condition exists that would warrant halting a maintenance operation (by assigning a value of $False). Otherwise, if one of the conditions being checked were to assign a value of $True, then it could override an earlier comparison that assigned a value of $False. Remember, in a real-world script, there would be numerous comparisons (start date, start time, end date, end time, day of the week, etc.). If any one of those comparisons yielded a value of $False then the maintenance task must not be started.

PowerShell building blocks for VMM servicing windows

My goal in this article was not to create an entire script that would check to see if the current time falls within a host’s servicing window, but rather to give you some of the building blocks that could be used to create such a script.

If you decide to create such a script, my advice would be to incorporate the code that I have given you (plus any additional comparisons that you might choose to add) into a PowerShell function. If the function’s $Go variable ultimately yields a value of $True, then you can call a secondary function to begin whatever maintenance task it is that you want to perform. If the function yields a $Go value of $False, however, then you can either abort the script or force the script to wait for a period of time and then perform a new comparison to see if it is now OK to perform maintenance on the host.

Featured image: Shutterstock / TechGenix photo illustration

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top