Create a Schedule Token for usage within SCCM

A PowerShell function that creates a recurring or non-recurring schedule token for usage within Configuration Manager. The output of this function is needed for the New-SMSServiceWindow function I have also posted but this function also relies on the function Convert-NormalDateToConfigMgrDate in order to create a usable schedule token. In short if you want to create a Maintenance Window and add this to a collection you will need a couple of functions (see another post for a function that combines a few to create a Maintenance Window). The base code of this function can be found on the blog of David O’Brien who outlined it really good. I have made some changes to the code to allow non-recurring Maintenance Windows and to allow for different time lengths.

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 or server name are blurred out (this is a real site server behind a FAKE FQDN).

Example that uses a Date/Time converted using the Convert-NormalDateToConfigMgrDate function and only -Verbose as a further parameter.
New-ScheduleToken Example NonRecurring

Example that uses a Date/Time converted using the Convert-NormalDateToConfigMgrDate function and in addition the duration options are specified for a 3,5 hour duration. Also the recurring option is specified with -Verbose to show Verbose output, notice the difference between SMS_ST_NonRecurring and SMS_ST_RecurInterval.
New-ScheduleToken Example Recurring

Code:

Function New-ScheduleToken
{
  <#
    .SYNOPSIS
    Creates a schedule token for usage with SCCM Maintenance Window(s)
    .DESCRIPTION
    This function creates a schedule token that is usable with other functions to set a maintenance window on a collection.
    If you do not specify a duration in either minutes, hours or days a fixed minimum of 5 minutes is used, otherwise specify all values
    
    .EXAMPLE
    New-ScheduleToken -StartDateTime <value from Convert-NormalDateToConfigMgrDate function> -DurationDay 1 -DurationHour 0 -DurationMinute 0
    .EXAMPLE
    New-ScheduleToken -StartDateTime <value from Convert-NormalDateToConfigMgrDate function> -DurationDay 0 -DurationHour 5 -DurationMinute 30

    .PARAMETER StartDateTime
    This parameter is a specific Configuration Manager DateTime provided through the function Convert-NormalDateToConfigMgrDate
    .PARAMETER DurationDay
    This parameter defines the duration in day(s)
    .PARAMETER DurationHour
    This parameter defines the duration in hour(s)
    .PARAMETER DurationMinute
    This parameter defines the duration in minute(s)
    .PARAMETER SiteServer
    This parameter defines the site server to connect to and is optional (default value in function)
    .PARAMETER SiteCode
    This parameter is optional and by default uses WMI to gather this based on the SiteServer parameter (or default of)
    .PARAMETER IsGMT
    This is a switch parameter that defines if the Schedule Token is in GMT or not (default $False)
    .PARAMETER Recurring
    This is a switch parameter that defines if the Schedule is recurring (default $False)
  #>
 Param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
    [string]$StartDateTime,
    [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]$DurationDay = 0,
    [Parameter(Mandatory=$false)]
    [int]$DurationHour = 0,
    [Parameter(Mandatory=$false)]
    [int]$DurationMinute = 5,
    [Parameter(Mandatory=$false)]
    [switch]$IsGMT = $False,
    [Parameter(Mandatory=$false)]
    [switch]$Recurring = $False
  )
  Write-Verbose "Start Date/Time (ConfigMgr Format) : $StartDateTime"
  Write-Verbose "Duration Hour(s)                   : $DurationHour"
  Write-Verbose "Duration Day(s)                    : $DurationDay"
  Write-Verbose "Duration Minute(s)                 : $DurationMinute"
  Write-Verbose "GMT Specified                      : $IsGMT"
  Write-Verbose "SCCM Site Server                   : $($SiteServer)"
  Write-Verbose "SCCM Site code                     : $($SiteCode)"  
  $class_SMS_ST = [wmiclass]"\\$($SiteServer)\ROOT\SMS\Site_$($SiteCode):SMS_ST_NonRecurring"
  If ($Recurring -eq $True)
  {
    # Below is used to create a recurring maintenance window by default every 1 days
    $class_SMS_ST = [wmiclass]"\\$($SiteServer)\ROOT\SMS\Site_$($SiteCode):SMS_ST_RecurInterval"
  }
  Write-Verbose "class_SMS_ST                       : $($class_SMS_ST)"
  $scheduleToken = $class_SMS_ST.CreateInstance()
  If ($scheduleToken)
  {
    $scheduleToken.DayDuration = $DurationDay
    $scheduleToken.HourDuration = $DurationHour
    $scheduleToken.IsGMT = $IsGMT
    $scheduleToken.MinuteDuration = $DurationMinute
    $scheduleToken.StartTime = $StartDateTime
    $class_SMS_ScheduleMethods = [wmiclass]"\\$($SiteServer)\ROOT\SMS\Site_$($SiteCode):SMS_ScheduleMethods"
    $script:ScheduleString = $class_SMS_ScheduleMethods.WriteToString($scheduleToken)
    [string]$ScheduleString.StringData
  }
}
Posted in Maintenance Window, PowerShell, System Center Configuration Manager, WMI.

2 Comments

  1. Pingback: Set Maintenance Window on an SCCM Collection using PowerShell – 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.