다음을 통해 공유


What’s new in R2? PowerShell and Maintenance Windows!

Greetings Everyone.  Big news this week for System Center folks as System Center 2012 R2 released.  As part of that, Configuration Manager 2012 R2 is available, and as a result of that…   New PowerShell cmdlets are available!   Maybe others don’t get as excited as I do when it comes to new Cmdlets, but it’s a party around the Lawson household when I get to explore new releases for new automation capabilities.

This post is part of a series that will cover what’s new in R2.  I’m sure we’ve all seen the awesome news from our product teams on this, but I’m going to take a different spin and put a focus on little improvements that make our lives as administrators easier.  This post is going to focus on our ability to add Maintenance Windows using PowerShell in R2.  This is a pretty welcome addition, as I’ve seen the code that goes into making Maintenance Windows a reality in previous releases, and it’s certainly much more than one line.  This is also amazingly powerful for automation scenarios, as we can programmatically control when clients will perform actions.

Side note here, while on the subject of Maintenance Windows, one other thing I am really excited for in R2 is the ability to define maintenance window usage.  With System Center Configuration Manager 2012 R2, we can now define separate Maintenance Windows for Software Updates, Operating System Deployment, and everything else.  Why is this important?  Picture the scenario:  I want my clients to install software updates during Maintenance Window A and applications to install during a Maintenance Window B.  In the past, the same maintenance window would have allowed both actions, which could lead to undesirable consequences.  In R2, I can define separate maintenance windows for each type of activity.  Pretty big deal, right?

image

Back to the subject, with Configuration Manager 2012 R2, we have new cmdlets to interact with Maintenance Windows.  Here, we’ll use the Get-Command cmdlet to find all Configuration Manager cmdlets that contain the word “MaintenanceWindow” :

 Get-Command –Module ConfigurationManager | where Name –like “*MaintenanceWindow”

Which gives us this: 

image

 Cool, now we've got an idea of the commands that are available.  Following the normal naming standard, each Powershell cmdlet follows Verb-Noun syntax.  Next up, let’s get some help on interacting with the New-CMMaintenanceWindow cmdlet using Get-Help:

 Get-Help New-CMMaintenanceWindow

 

image

It appears we need to give it a name and a CollectionID to bind the maintenance window to.  This seems easy enough – but wait, what’s that Schedule it’s looking for?  Well, as it turns out it’s looking for a ScheduleToken, which we can quickly create using New-CMSchedule cmdlet:

 Get-Help New-CMSchedule

image

Easy enough, looks like I just have to feed it a start and end time., though I do have to do just a bit of formatting to make sure I feed it a start and end date.  In this case, we’ll take the easy road and let .NET do the formatting for us:

  #Use the DateTime.Parse() Method to parse in date to a DateTime 
 $StartTime = [DateTime]::Parse('2013-10-20 05:00') 
 $EndTime = [DateTime]::Parse('2013-10-20 08:00')
 
 #Create The ScheduleToken 
 New-CMSchedule -Nonrecurring -Start $StartTime -End $EndTime

 

Scream it from the mountain tops, we have a schedule token!  Next, let’s pull it all together and create our maintenance window:

  #Parameters 
 $CollectionID = "P200000A"
 
 #Use the DataTime.Parse() Method to parse in date to a DateTime 
 $StartTime = [DateTime]::Parse('2013-10-20 05:00') 
 $EndTime = [DateTime]::Parse('2013-10-20 08:00')
 
 #Create The ScheduleToken 
 $Schedule = New-CMSchedule -Nonrecurring -Start $StartTime -End $EndTime
 
 New-CMMaintenanceWindow -CollectionID $CollectionID -Schedule $Schedule -Name "Heath's Sunday Maintenance"
 

When we run the example above, we end up with this:

image

Hopefully this post has armed you with the knowledge you need to programmatically create maintenance windows.  Can you think of scenarios where this code can be used for new automation abilities?  Do you have an awesome use case in mind or feedback?  Let me hear it in the comments below!