A look into Servicing Windows in Virtual Machine Manager 2012 (via PowerShell)

Servicing Windows, which are a new feature available in VMM 2012 are given little attention in product documentation, perhaps because they don’t really have a direct use in the VMM 2012 UI. Servicing Windows allow you to establish the windows of time during which running hosts, VMs and services (the variety created from service templates) can be safely serviced (patched, settings updated, scaled out, etc.). However, servicing windows are a great way to advertise your maintenance schedule in the private cloud for consumption by orchestrated maintenance workflows in external applications, such as Orchestrator 2012.

In this post, we’ll examine how to automate service window creation, subscription and evaluation for use automated maintenance workflows.

  • Creating a Servicing Window
  • Applying Servicing Windows to Cloud Assets (Hosts, Guest, Services)
  • Determining if you are within the Servicing Window
  • Additional Resources

Creating a Servicing Window and Applying to Cloud Assets 

To use and implement a servicing window requires two steps:

  1. Create the servicing window
  2. associate (subscribe) one or more managed objects to that servicing window (VM hosts, VM guests, services).

In this example, we will create a servicing window that occurs every Saturday at 1 AM and lasts for two hours. Then, will associate that servicing window with all the Hyper-V hosts in the ‘Contoso Lab’ host group. The power shall to create this is relatively simple. One additional piece around front you a require is a list of valid TimeZone settings for the –TimeZone parameter of the New-SCServicingWindow cmdlet. A link is provided immediately below the sample script.

#Step 1: Create a Servicing Window 

#Get the date one week in the future and store in variable 

$Date = (Get-Date).AddDays(7) 

 

#Create a new servicing window that occurs every Saturday for 2 hours starting at 1am 

$NewSvcWindow = New-SCServicingWindow -Name "Saturday Hyper-V Maintenance" -Category "Production Hosts" `

-StartDate $Date -StartTimeOfDay "1:00" -TimeZone 035 -WeeklyScheduleDayOfWeek "Saturday" -WeeksToRecur 1 -MinutesDuration 120

#step 2: Subscribe Hyper-V hosts, VMs or Services to the window

$VMHosts = get-SCVMHost | where {$_.VMHostGroup -eq 'Contoso Lab'}

 

Foreach($VMHost in $VMHosts) {

 

 Add-SCServicingWindowSubscription -ServicingWindow $NewSvcWindow -VMHost $VMHost

 

}

IMPORTANT: If you look at the –TimeZone parameter in the code above, you will notice a numeric value for the time zone the servicing window is set to. For  a complete list of time zone values, see “Microsoft Time Zone Index Values”  at http://support.microsoft.com/kb/973627

Determining if you are within the Servicing Window

You can also query the settings of the servicing window via PowerShell to determine if you are within the servicing window of your cloud assets BEFORE you begin maintenance. The The servicing window object has a number of useful properties and methods. Which are useful depends on the characteristics of your maintenance window (weekly, monthly, days-of-week, etc.). For purposes of our discussion, we will work with the Saturday 1am.  Since every maintenance window has a start date, I’ll have to check the start date on the maintenance window in addition to the day of the week , hour of the day  and window duration. will start by looking at the available properties and methods of a service window object  in PowerShell.

$NewSvcWindow | get-member 

(where $NewSvcWindow is the servicing window created in the PowerShell snippet above)

The important properties and methods for purposes of our discussion are listed here.

Methods Properties
Updated
CompareTo
Equals
GetHashCode

GetStartDateTimeInLocalTimeZone

GetType
OnAddedToCache
ToString
Category
DayOfMonth
DaysToRecur
Description
ID
IsFullyCached
IsViewOnly
MarkedForDeletion
MinutesDuration
MonthlyScheduleDayOfWeek
MonthsToRecur
Name
ObjectType
Owner
ScheduleType
ServerConnection
StartDate
StartTimeOfDay
TimeZone
WeeklyScheduleDayOfWeek
WeekOfMonth
WeeksToRecur

 

Building on the new servicing window we created (and stored in the $NewSvcWindow variable), we can retrieve some of these key properties and make some comparisons. Since we know we’re dealing with a weekly servicing window, I’m not going through the steps of determining what type of servicing window I’m dealing with.

Is planned start time later than the effective start date of the servicing window?

The servicing  window has a start date (in our example 7 days from today), which we can validated against the date. We could retrieve this using the GetStartDateTimeInLocalTimeZone() method, which returns the effective start date and the time of the servicing window. Since we know we’re working with a weekly schedule and I wanted to poke around into the properties of the servicing window a bit, we’ll go a different route.

For the sake of exploration, let’s look at the individual date and time-related properties instead.

Are we at or beyond the effective start date of the servicing window?

If we retrieve StartDate property of the the servicing window, we can compare to the current date to see if we’re within the maintenance

#StartDate property is a string, so force to DateTime format for comparison

[DateTime]$StartDate = $NewSvcWindow.StartDate  

 

#Get the current date 

$CurrDate = get-date

 

#Make sure current date is >= StartDate of servicing window

if ($CurrDate.Date -gt $StartDate) {$StartMaint = $true}

elseif ($CurrDate.Date -gt $StartDate) {$StartMaint = $true}

else {$StartMaint = $false}

write-host $StartMaint

Is this day of the week included in the servicing window?

The WeeklyScheduleDayOfWeek property holds this information, and we can compare to the

$NewSvcWindow.WeeklyScheduleDayOfWeek

Now, we can compare to the current day of week to see if this day is included in our servicing window.

#Retrieve current date and date of week

$CurrDate = get-date

$Day = $CurrDate.DayOfWeek

If ($NewSvcWindow.WeeklyScheduleDayOfWeek –match ‘$Day’) {$StartMaint = $true}
else {$StartMaint = $false}

Is the time equal to or later than the start time?

We can retrieve time-related info from the However, when you retrieve the StartTimeOfDay property, the output is a bit unexpected to the other extreme versus the output of GetStartDateTimeInLocalTimeZone(). It looks like what you see below.

Days              : 0
Hours             : 1
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 36000000000
TotalDays         : 0.0416666666666667
TotalHours        : 1
TotalMinutes      : 60
TotalSeconds      : 3600
TotalMilliseconds : 3600000

This led me to retrieve the start hour and minute instead and to do my comparison that way, like so and then we can compare it to current time

#Get start time of the service window 

$StartHour = $NewSvcWindow.StartTimeOfDay.Hour

$StartMin = $NewSvcWindow.StartTimeOfDay.Minute

$WindowStartTime = ("$StartHour" + ':' + "$StartMin")

 

#Get current time of day

$CurrTime = ([string](get-date).hour + ":" + [string](get-date).minute)

 

#Compare window start time to current time. If current time >= window start time

#then we are safe to begin maintenance 

if ($CurrTime -eq $WindowStartTime) {$StartMaint = $true}

elseif ($CurrTime -gt $WindowStartTime) {$StartMaint = $true}

else {$StartMaint = $false}

What is the duration of this servicing window? Are we starting too late?

The MinutesDuration property of the servicing window object we created earlier ($NewServiceWindow), we can determine if the current time places us within the servicing window.

#Calculate end time of the servicing window
$Duration = $NewServiceWindow.MinutesDuration
$WindowStartTime = [DateTime]([string](get-date).hour + ":" + [string](get-date).minute)
$WindowEndTime = $WindowStartTime.AddMinutes($Duration)



#Compare this to current time to determine if still within servicing window
if ($CurrTime -lt $WindowEndTime) {$StartMaint = $true}
else {$StartMaint = $false}

Hopefully this gives you an appreciation for how you can leverage servicing windows in your maintenance automation scenarios.

Additional Resources

Here are a few additional private cloud-related posts featuring VMM 2012 and related technologies that may be of interest

On TechNet blogs

VMM 2012 Update Management- Getting Started with Sample Baselines (Part 1 of 2)

Automating Update Server Synchronization in VMM 2012(Part 2 of 2)

On System Center Central

Configuring PRO integration between OpsMgr 2012 and VMM 2012

[Private Cloud]: Configuring anti-affinity between VMs on Hyper-V 2008 R2 SP1 and VMM 2012

Implementing rapid scale-out of a machine tier in VMM 2012 via PowerShell

Cloud: A quick note on accurately measuring Hyper-V host performance and utilization

What’s new in the Cloud Services Process Pack (CSPP) Release Candidate for System Center 2012?

Private Cloud in the real world: 5 lessons from the private cloud fabric

How to prepare for Orchestrator? Learn Opalis in a month of Friday afternoons! (free online resources)

PowerShell Scripts for System Center (Master Collection)