Sample #PowerShell code for my TechEd Session – Demo 2 – Create Collections and Maintenance Windows for Server Patching.

Here’s an example of creating collections (with maintenance windows) for server patching.

Start with a .csv of servers, and the desired patch window (download sample csv).

You then run the PowerShell script to group the systems and populate new collections.
Download Script…

(if you’re having trouble with copy/paste of the code, or if code doesn’t look correct (with line feed, etc), please try an alternate browser. . .:( )


#Demo 2 - Software Update Scenario
$CMModulePath = $Env:SMS_ADMIN_UI_PATH.ToString().SubString(0,$Env:SMS_ADMIN_UI_PATH.Length - 5) `
    + "\ConfigurationManager.psd1"
Import-Module $CMModulePath -force
cd PR1:

#Removing Old Collections that begin with 'Serverpatch'
Get-CMDeviceCollection| Where-Object {$_.name -like "serverpatch*"} | Remove-CMDeviceCollection -force

#import the csv of servers with patch window
$Servers = import-csv C:\scripts\ServerPatchWindows.csv

#use the group-object cmdlet to group servers into patch windows
$Servers | Group-Object Patchwindow | ForEach-Object {
    #Gives us a window name like ServerPatch_5_26_2014_20_00
    $PatchWindowName = "ServerPatch_" + $_.Name -replace "[:\./ ]", "_"
    $PatchWindowName  

    "Creating Collection {0}" -f $PatchWindowName
    $NewColl = New-CMDeviceCollection -Name $PatchWindowName -LimitingCollectionName "All Servers" 

    #Create Schedule for 5 hour MW
    $Schedule = New-CMSchedule -Nonrecurring  -Start ([datetime]$_.Name) -durationinterval Hours -DurationCount 5

    #Create MW
    New-CMMaintenanceWindow -Name ("Maint" + $PatchWindowName) -ApplyToSoftwareUpdateOnly `
        -CollectionID $NewColl.CollectionID -Schedule $Schedule
    $Schedule = $null

    #Create Server list separated by commas
    $_.Group | foreach {
       $string = $string + '"' + $_.Servername + '",'
    }

    #build the WQL and remove the extra comma
    $wql = "select *  from  SMS_R_System where SMS_R_System.Name in (" + $string.trimend(',') + ")"
    Add-CMDeviceCollectionQueryMembershipRule -CollectionId $NewColl.CollectionID `
        -RuleName "Query1" -QueryExpression $wql
    $string = $null
}

Download Script…

Happy Scripting!

Greg

This post first appeared on http://www.gregramsey.net

About Greg Ramsey
Greg Ramsey is a systems engineer specializing in global systems management for Dell IT. He has a B.S. in Computer Sciences and Engineering from the Ohio State University and is a Microsoft Most Valuable Professional (MVP) for Microsoft System Center Configuration Manager. Greg coauthored SMS 2003 Recipes: A Problem-Solution Approach (Apress, 2006), Microsoft System Center Configuration Manager Unleashed (Sams, 2009), Microsoft System Center 2012 Configuration Manager Unleashed (Sams, 2012), and Microsoft System Center 2012 Configuration Manager: Administration Cookbook (Packt Publishing, 2012), and Microsoft System Center 2012 Configuration Manager R2 Unleashed (Sams, 2012). Greg is cofounder of the Ohio SMS Users Group, and the Central Texas Systems Management User Group.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: