Set Maintenance Window on an SCCM Collection using PowerShell

This function creates a new Service Window/Maintenance Window for a Configuration Manager collection all using WMI so you do not need the Configuration Manager cmdlets which sometimes can be frustrating to use. This function requires a schedule token as input which can be created using the New-ScheduleToken function. The base code of this function can be found on the blog of David O’Brien who used this in his general function to create a Maintenance Window.

NOTE: By including the SYNOPSIS and other information you can use get-help on the function to find out what parameters you can specify or read what this function actually can be used for. I can highly recommended to always include this information in your PowerShell function(s) so that anybody can understand it.

Examples:
In the below examples names pointing to a sitecode, collection ID or server name are blurred out (this is a real site server behind a FAKE FQDN).

The example below first uses 3 functions together to create the Maintenance Window.
First the Convert-NormalDateToConfigMgrDate function is used to create a usable Date/Time format.
Secondly the Convert-NormalDateToConfigMgrDate output is used for the New-ScheduleToken function to create the Schedule Token.
Finally the schedule token is used with the New-SMSServiceWindow function to create the Maintenance Window on the collection.
New-SMSServiceWindow Example

If you open the properties of the collection and there select the Maintenance Windows tab you can see the newly created Maintenance Window.
SCCM Collection Maintenance Window

If you open the properties of the Maintenance Window you can see that the configuration we specified (month, year) and the duration are correctly configured as is the fact that this Maintenance Window is non-recurring.
SCCM Maintenance Window Properties

Code:

Function New-SMSServiceWindow
{
  <#
    .SYNOPSIS
    This is based on the SMS_ServiceWindow class and it defines the maintenance/service window to be embedded in the SMS_CollectionSettings instance.
    .DESCRIPTION
    This function creates a new service window (Maintenance Window) on the specified collection, it does need input from the New-ScheduleToken function to work.

    .EXAMPLE
    New-SMSServiceWindow -MaintenanceWindowName "My Maint Window" -MaintenanceWindowDescription "Test" -SCCMScheduleString <from New-ScheduleToken function> -SCCMCollectionID <sccm collection ID>
    
    .PARAMETER MaintenanceWindowName
    This parameter is the name of the Maintenance Window set on the collection
    .PARAMETER MaintenanceWindowDescription
    This parameter is the description of the Maintenance Window
    .PARAMETER SCCMCollectionID
    This parameter is the CollectionID on which the Maintenance Window is set
    .PARAMETER SCCMScheduleString
    This parameter is the value as returned by the New-ScheduleToken function
    .PARAMETER SCCMServiceWindowType
    This parameter is the type of the service window (default is 4 for Software Updates)
    .PARAMETER StartDateTime
    This a standard get-date Date/Time format which is converted to a value usable by Configuration Manager (if not specified current date/time is used)
    .PARAMETER SiteServer
    This parameter defines the site server to connect to and is optional
    .PARAMETER SiteCode
    This parameter is optional and by default uses WMI to collect this from the Site Server
  #>
 Param(
    [Parameter(Mandatory=$true)]
    [string]$MaintenanceWindowName,
    [Parameter(Mandatory=$True)]
    [string]$SCCMCollectionID,
    [Parameter(Mandatory=$True)]
    [string]$SCCMScheduleString, 
    [Parameter(Mandatory=$false)]
    [string]$SiteServer = "mysiteserver.example.com",
    [Parameter(Mandatory=$false)]
    [string]$SiteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $SiteServer).SiteCode,
    [Parameter(Mandatory=$false)]
    [int]$SCCMServiceWindowType = 4, # https://msdn.microsoft.com/library/jj155419.aspx?f=255&MSPPError=-2147217396
    [Parameter(Mandatory=$false)]
    [datetime]$StartDateTime = (Get-Date),
    [Parameter(Mandatory=$false)]
    [string]$MaintenanceWindowDescription
  )
  Write-Verbose "SCCM Site Server                   : $($SiteServer)"
  Write-Verbose "SCCM Site code                     : $($SiteCode)"
  Write-Verbose "SCCM Collection ID                 : $($SCCMCollectionID)"
  If (!($MaintenanceWindowDescription))
  {
    $MaintenanceWindowDescription = "Created on $($StartDateTime.Year)-$($StartDateTime.Month)-$($StartDateTime.Day)"
  }
  Write-Verbose "Maintenance Window Name            : $($MaintenanceWindowName)"
  Write-Verbose "Maintenance Window Description     : $($MaintenanceWindowDescription)"
  Write-Verbose "Start Date/Time                    : $StartDateTime"
  $ServiceWindowStartDateTime = $(Get-Date $StartDateTime -Format "yyyyMMddhhmmss.ffffff+***")
  Write-Verbose "SCCM Format of specified Date/Time : $ServiceWindowStartDateTime"
  $CollSettings = Get-WmiObject -Class "SMS_CollectionSettings" -Namespace "ROOT\SMS\Site_$($SiteCode)" -Filter "CollectionID = '$($SCCMCollectionID)'" -ComputerName $SiteServer
  If ($CollSettings)
  {
    $CollSettings = [wmi]$CollSettings.__PATH
    $CollSettings.Get()
    $class_SMS_ServiceWindow = [wmiclass]"\\$($SiteServer)\ROOT\SMS\Site_$($SiteCode):SMS_ServiceWindow"
    $SMS_ServiceWindow = $class_SMS_ServiceWindow.CreateInstance()
    $SMS_ServiceWindow.Name                     = "$($MaintenanceWindowName)"
    $SMS_ServiceWindow.Description              = "$($MaintenanceWindowDescription)"
    $SMS_ServiceWindow.IsEnabled                = $true
    $SMS_ServiceWindow.ServiceWindowSchedules   = $SCCMScheduleString
    $SMS_ServiceWindow.ServiceWindowType        = $SCCMServiceWindowType
    $SMS_ServiceWindow.StartTime                = "$($ServiceWindowStartDateTime)"
    $CollSettings.ServiceWindows += $SMS_ServiceWindow.psobject.baseobject
    $CollSettings.Put() |Out-Null
  }
}

Posted in Maintenance Window, PowerShell, System Center Configuration Manager, WMI.

2 Comments

  1. Pingback: Create a Schedule Token for usage within SCCM – Bits and bytes from a Windows Engineer

  2. Pingback: Function to create and set a Maintenance Window on a specified SCCM Collection – Bits and bytes from a Windows Engineer

Leave a Reply

Your email address will not be published.