For the last two years, we’ve just had workstations in our SCCM environment, and now we are starting to bring in the servers. The server teams have a patching schedule and there are quite a few of them. They also change every month, they always relative to Patch Tuesday and have different reboot times. As a solution, we’ve setup a folder to put all the collections in and use a naming convention of “Patch Day NN ???? Reboot HH:MM”
So, I have a script I use, I’m publishing it now, but I confess, I want to do some polishing. I’d like to make it a little more efficient, but in the meantime, here it is. You can download it from my bitbucket link, or copy and paste from below. I’ve tried to put as many comments in there as I could, when I update the script, I’ll repost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
<# .SYNOPSIS This script sets non-repeating maintenance windows for patches that are realtive to Patch Tuesday on collections in a folder based on their name for the current month. .DESCRIPTION This script will get all the collections in a folder, if they match a naming convention of "Patch Day NN ??? Reboot NN:NN AM" it will set a non-repeating maintenance window on the collection. Considerations: The maintenance window will ALWAYS start on the relative day, so if you put Patch Day 01, the maintenance window will start on Patch Tuesday. If the reboot time is at 3:00am and you are using a 4 hour MaintenanceWindowDurration, the window will start on the relative day and reboot at 3:00am on the FOLLOWING day. I confess I haven't tried every possible combination for the collection names, so errors could result, but if you follow the pattern shown here, you should be succesfull I log the setting of the maintenance windows as a warning, so if use the defaults, only the Maintenance window changes would be logged. When the maintenance window is set, any existing maintenance windows will be deleted.+ .PARAMETER SiteServer ConfigMGR site server. This is where the SMS Provider is installed to connect to the site database .PARAMETER MaintenanceWindowDuration Duration, in hours, to make the maintenance window. The default is 4, but can be from 1 to 6 hours .PARAMETER LogLevel Sets the minimum logging level, default is 1. 1 - Informational 2 - Warning 3 - Error .PARAMETER LogFileDir Directory where you want the log file. Defaults to C:\Temp\ .PARAMETER ClearLog This will clear the log if it exists .EXAMPLE Set-CMNSUMaintenanceWindow -SiteServer CMNSVR01 -MaintenanceWindowDuration 5 -ClearLog This would work on CMNSVR01 and set the maintenance windows that are 5 hours long. It would also clear the log file if it existed .NOTES .LINK http://configman-notes.com #> PARAM ( [Parameter(Mandatory=$true,HelpMessage='Site Server')] [ValidateScript({Test-Connection -ComputerName $_ -Count 1 -Quiet})] [String]$SiteServer, [Parameter(Mandatory=$true,HelpMessage='Container to set schedules in')] [String]$ContainerName, [Parameter(Mandatory=$false,HelpMessage='Duration of maintenance window in hours (1 to 6)')] [ValidateRange(1,6)] [Int32]$MaintenanceWindowDuration=4, #Parameters in script for New-LogEntry [Parameter(Mandatory=$false,HelpMessage="Sets the Log Level, 1 - Informational, 2 - Warning, 3 - Error")] [Int32]$LogLevel = 2, [Parameter(Mandatory=$false,HelpMessage="Log File Directory")] [String]$LogFileDir = 'C:\Temp\', [Parameter(Mandatory=$false,HelpMessage="Clear any existing log file")] [Switch]$ClearLog ) #Begin New-LogEntry Function Function New-LogEntry { # Writes to the log file Param ( [Parameter(Position=0,Mandatory=$true)] [String] $Entry, [Parameter(Position=1,Mandatory=$false)] [INT32] $type = 1, [Parameter(Position=2,Mandatory=$false)] [String] $component = $ScriptName ) Write-Verbose $Entry if ($type -ge $Script:LogLevel) { if ($Entry.Length -eq 0) { $Entry = 'N/A' } $TZOffset = ([TimeZoneInfo]::Local).BaseUTcOffset.TotalMinutes $TZOffset = "$(Get-Date -Format "HH:mm:ss.fff")+$(-$TZOffset)" $Entry = "<![LOG[{0}]LOG]!><time=""{2}"" date=""{1}"" component=""{5}"" context="""" type=""{4}"" thread=""{3}"">" -f $Entry, (Get-Date -Format "MM-dd-yyyy"), $TZOffset, $pid, $type, $component $Entry | Out-File $Script:LogFile -Append -Encoding ascii } } #End New-LogEntry Function Get-CMNMWStartTime { <# .SYNOPSIS This function will calculate the Maintenance Window Start Time .DESCRIPTION Using the collection name, it will start the Maintenance Window on the day relative to Patch Tuesday. It will set it to end on the time the collection reboot time. .PARAMETER Collection Collection name to figure out the maintenance window for. .PARAMETER MaintenanceWindowDuration This is how long, in hours, your maintenance window is. This can be from 1 to 6 hours, default is 4. The validation check is done in the initial set of parameters, I did not do it here also. .PARAMETER PatchTuesday The date for Patch Tuesday .EXAMPLE $StartDateTime = Get-CMNMWStartTime 'Patch Day 03 - Reboot 03:00' 4 $PatchTuesday This will get the startdate for a collection Named Patch Day 03 - Reboot 03:00. It will have a four hour duration and end at 3:00AM #> PARAM ( [Parameter(Mandatory=$True)] [String]$Collection, [Parameter(Mandatory=$True)] [Int32]$MaintenanceWindowDuration, [Parameter(Mandatory=$True)] [DateTime]$PatchTuesday ) $Collection -match '\d{1,2}:\d{1,2}.?([AP]M)' | Out-Null $Meridian = $Matches[1] $Collection -match 'Patch Day ([0-9]*).*' | Out-Null [int32]$Day = $Matches[1] $Collection -match '(\d{1,2}):(\d{1,2})' | Out-Null [Single]$Mn = $Matches[2] [Single]$Hr = $Matches[1] if(($Hr -lt 12) -and ($Meridian -eq 'PM')) { $Hr += 12 if($Hr -ge 24){$Hr -= 24} } $DecTime = $Hr + ($Mn / 60) $StartDateTIme = $PatchTuesday.AddDays($Day) $StartDateTIme = $StartDateTIme.AddHours($Hr - $MaintenanceWindowDuration) $StartDateTIme = $StartDateTIme.AddMinutes($Mn) if($DecTime -gt $MaintenanceWindowDuration){$StartDateTIme = $StartDateTIme.AddDays(-1)} Return $StartDateTIme } #End Get-CMNMWStartTime Function Get-CMNPatchTuesday { <# .SYNOPSIS Calculates Patch Tuesday for the current month .DESCRIPTION See Synopsis .EXAMPLE $PatchTuesday = Get-CMNPatchTuesday #> #Calculate Patch Tuesday Date [DateTime]$StrtDate = Get-date("$((Get-Date).Month)/1/$((Get-Date).Year)") While($StrtDate.DayOfWeek -ne 'Tuesday'){$StrtDate = $StrtDate.AddDays(1)} #Now that we know when the first Tuesday is, let's get the second. $StrtDate = $StrtDate.AddDays(7) Return $StrtDate } #End Get-CMNPatchTuesday Function Get-CMNObjectContainerNodeID { <# .SYNOPSIS Returns the ContainerNodeID for the requested Container .DESCRIPTION Passing the ContainerName and the Conainer type (See notes for various types), this will return the ConatinerNodeID. WARNING - May return more than one ID! This would usually indicate you have the same folder name in two different locations. .EXAMPLE Get-CMNObjectContainerNodeID -Name 'FolderName' -ObjectTypeID 5000 This will retreive the ContainerNodeID for the Device Collection folder 'FolderName' .PARAMETER Name This is the name of the Conatiner you are trying to get the ID of. .PARAMETER ObjectTypeID This is the ObjectTypeID (See note below) to filter the type of container you are looking for. .NOTES SMS_ObjectContainerNode - Maps Folder name to ConatainerNodeID ObjectTypes: 2 - SMS_Package 3 - SMS_Advertisement 7 - SMS_Query 8 - SMS_Report 9 - SMS_MeteredProductRule 11 - SMS_ConfigurationItem 14 - SMS_OperatingSystemInstallPackage 17 - SMS_StateMigration 18 - SMS_ImagePackage 19 - SMS_BootImagePackage 20 - SMS_TaskSequencePackage 21 - SMS_DeviceSettingPackage 23 - SMS_DriverPackage 25 - SMS_Driver 1011 - SMS_SoftwareUpdate 2011 - SMS_ConfigurationBaselineInfo 5000 - SMS_Collection_Device 5001 - SMS_Collection_User 6000 - SMS_ApplicationLatest 6001 - SMS_ConfigurationItemLatest SMS_ObjectContainerItem - Maps ContainerNodeID to CollectionID .LINK http://configman-notes.com #> PARAM ( [Parameter(Mandatory=$true,HelpMessage='Name of container to locate')] [string]$Name, [Parameter(Mandatory=$true)] [ValidateSet('2','3','7','8','9','11','14','17','18','19','20','21','23','25','1011','2011','5000','5001','6000','6001')] [String]$ObjectTypeID ) $ContainerNodeID = (Get-WmiObject -Class SMS_ObjectContainerNode -Filter "Name = '$Name' and ObjectType = '$ObjectTypeID'" @WMIQueryParameters).ContainerNodeID Return $ContainerNodeID } #End Get-CMNObjectContainerNodeID #Build variables for New-LogEntry Function $ScriptName = $MyInvocation.MyCommand.Name if(-not ($LogFileDir -match '\\$')){$LogFileDir = "$LogFileDir\"} $LogFile = $ScriptName -replace '(.*)\.ps1', '$1' $LogFile = $LogFileDir + $LogFile + '.log' if($ClearLog) { if(Test-Path $Logfile) {Remove-Item $LogFile} } New-LogEntry 'Starting Script' New-LogEntry "SiteServer - $SiteServer" New-LogEntry "ContainerName - $ContainerName" New-LogEntry "MaintenanceWindowDuration - $MaintenanceWindowDuration" New-LogEntry "LogLevel - $LogLevel" New-LogEntry "LogFileDir - $LogFileDir" New-LogEntry "ClearLog - $ClearLog" #Get the SiteCode $SiteCode = $(Get-WmiObject -ComputerName $SiteServer -Namespace 'root\SMS' -Class SMS_ProviderLocation).SiteCode #Build the WMIQueryParameters Hash Table for later use. $WMIQueryParameters = @{ ComputerName = $SiteServer Namespace = "root\sms\site_$SiteCode" } $PatchTuesday = Get-CMNPatchTuesday New-LogEntry "Patch Tuesday is on the $PatchTuesday" #Get the ContainerNodeID, if we have an error, we want to catch that. try { #Get list of Collection ID's $FolderID = Get-CMNObjectContainerNodeID $ContainerName 5000 } catch [System.Exception] { New-LogEntry "Unable to find Container $ContainerName" 3 throw "Unknown Container - $ContainerName" } #Now, get the CollectionID's of all the collections in that folder. $CollectionIDs = (Get-WmiObject -Class SMS_ObjectContainerItem -Filter "ContainerNodeID = '$FolderID' and ObjectType = 5000" @WMIQueryParameters).InstanceKey #Cycle through list foreach($CollectionID in $CollectionIDs) { #Get a Collection in an object, check the name to see if it meets our requirements $Collection = Get-WmiObject -Class SMS_Collection -Filter "CollectionID = '$CollectionID'" @WMIQueryParameters if(($Collection.Name -match 'Patch Day ([0-9]*).*') -and ($collection.Name -match 'Reboot\s?(\d+:\d+)')) { try { #Get MW Start Time $StartDateTime = Get-CMNMWStartTime $Collection.Name $MaintenanceWindowDuration $PatchTuesday #Create CMSchedule and ServiceWindow Objects $CMSchedule = ([WMIClass]"\\$($SiteServer)\Root\sms\site_$($SiteCode):SMS_ST_NonRecurring").CreateInstance() $ServiceWindow = ([WMIClass]"\\$($SiteServer)\Root\sms\site_$($SiteCode):SMS_ServiceWindow").CreateInstance() #Specify Schedule $CMSchedule.DayDuration = 0 $CMSchedule.HourDuration = $MaintenanceWindowDuration $CMSchedule.IsGMT = $false $CMSchedule.MinuteDuration = 0 $CMSchedule.StartTime = $ServiceWindow.ConvertFromDateTime($StartDateTIme) #We need to get the SMS_CollectionSettings instance for this collection, however, if it's never had a schedule, it won't have one try { $CollectionSettings = Get-WmiObject -Class SMS_CollectionSettings -Filter "CollectionID = '$CollectionID'" @WMIQueryParameters $CollectionSettings.Get() } #This will catch the ones that haven't had a MW. catch [System.Exception] { New-LogEntry "Creating $($Collection.Name) CollectionSettings instance" 2 $CollectionSettings = ([WMIClass]"\\$($SiteServer)\Root\sms\site_$($SiteCode):SMS_CollectionSettings").CreateInstance() $CollectionSettings.CollectionID = "$CollectionID" } $ServiceWindow.Name = ($Collection.Name) $ServiceWindow.Description = ($Collection.Name) $ServiceWindow.IsEnabled = $true $ServiceWindow.ServiceWindowSchedules = (Invoke-WmiMethod -Name WriteToString -Class SMS_ScheduleMethods @WMIQueryParameters $CMSchedule).StringData #Now, we're duplicating some data, but such is life. $ServiceWindow.Duration = ($MaintenanceWindowDuration * 60) #This says non-repeating $ServiceWindow.RecurrenceType = 1 #This is for updates only $ServiceWindow.ServiceWindowType = 4 $ServiceWindow.StartTime = $ServiceWindow.ConvertFromDateTime($StartDateTIme) #And we set it $CollectionSettings.ServiceWindows = $ServiceWindow.PSObject.BaseObject New-LogEntry "Setting $($Collection.Name) - Maintenance Window. Starts at $StartDateTIme and goes for $MaintenanceWindowDuration hours for Software Updates only." 2 #Final step, it's just like we're hitting the "OK" button! $CollectionSettings.put() | Out-Null } #We hope this never runs! catch [System.Exception] { New-LogEntry "Unable to update $($Collection.Name)" 3 New-LogEntry $Error[0].ToString() } } } New-LogEntry 'Finished Script!' |