ConfigMgr 2007 and creating a non-recurring Maintenance Window by script

At my current customer they’re not using the Software Updates of ConfigMgr 2007 (yet), but there was a wish for a more controlled company-wide deployment without having to change all the current advertisements (and the whole deployment system). So the idea came to create (and delete) maintenance windows by script (when needed).

Luckily the ConfigMgr 2007 SDK has some pretty straight forward examples of creating and deleting maintenance windows (see also the links at the end of this post). Deleting a maintenance window was almost just copy-paste from the SDK, the tricky part was creating a maintenance window and then especially the non-recurring schedule. At the end, this is (the short version of) what we ended up with:

‘====================================================
‘ MAIN – Set connection, schedule and call
‘====================================================
Set Connection = ConnectToSMSProvider("SiteServerName")
Schedule = NonRecurringScheduleString(connection, 3, “StartTimeInWMIFormat”, FALSE)
CreateMaintenanceWindow(connection, "CollectionID", "Name of Maintenance Window", "", Schedule, TRUE, 1)

‘====================================================
‘ Sub to add a Maintenance Window to a Collection
‘====================================================
Sub CreateMaintenanceWindow(connection, targetCollectionID, newMaintenanceWindowName, newMaintenanceWindowDescription, newMaintenanceWindowServiceWindowSchedules, _
                            newMaintenanceWindowIsEnabled, newMaintenanceWindowServiceWindowType)
    Set allCollectionSettings = connection.ExecQuery("Select * From SMS_CollectionSettings Where CollectionID = ‘" & targetCollectionID & "’")
    If allCollectionSettings.Count = 0 Then
        Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings").SpawnInstance_
        collectionSettingsInstance.CollectionID = targetCollectionID
        collectionSettingsInstance.Put_
    End If 
    Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings.CollectionID=’" & targetCollectionID &"’" )
    Set tempServiceWindowObject = connection.Get("SMS_ServiceWindow").SpawnInstance_
    tempServiceWindowObject.Name = newMaintenanceWindowName
    tempServiceWindowObject.Description = newMaintenanceWindowDescription
    tempServiceWindowObject.ServiceWindowSchedules = newMaintenanceWindowServiceWindowSchedules
    tempServiceWindowObject.IsEnabled = newMaintenanceWindowIsEnabled
    tempServiceWindowObject.ServiceWindowType = newMaintenanceWindowServiceWindowType
    tempServiceWindowArray = collectionSettingsInstance.ServiceWindows
    ReDim Preserve tempServiceWindowArray (Ubound(tempServiceWindowArray) + 1)
    Set tempServiceWindowArray(Ubound(tempServiceWindowArray)) = tempServiceWindowObject
    collectionSettingsInstance.ServiceWindows = tempServiceWindowArray
    collectionSettingsInstance.Put_
End Sub

‘====================================================
‘ Function to RETURN a Non Recurring Schedule
‘====================================================
Function NonRecurringScheduleString(connection, hourDuration, startTime, isGmt)
    Set recurInterval = connection.Get("SMS_ST_NonRecurring").SpawnInstance_()
    recurInterval.StartTime = startTime
    recurInterval.DayDuration = 0
    recurInterval.HourDuration = hourDuration
    recurInterval.MinuteDuration = 0
    recurInterval.IsGMT = isGmt
    Set clsScheduleMethod = connection.Get("SMS_ScheduleMethods")
    clsScheduleMethod.WriteToString Array(recurInterval), scheduleString
    NonRecurringScheduleString = scheduleString
End Function

‘====================================================
‘ Function to RETURN a Date/Time in WMI Format
‘====================================================
Function ConvertToWMIDate(strDate)
    strYear = year(strDate):strMonth = month(strDate)
    strDay = day(strDate):strHour = hour(strDate)
    strMinute = minute(strDate)
    If len(strmonth) = 1 Then strMonth = "0" & strMonth
    If len(strDay) = 1 Then strDay = "0" & strDay
    If len(strHour) = 1 Then strHour = "0" & strHour
    If len(strMinute) = 1 Then strMinute = "0" & strMinute
    ConvertToWMIDate = strYear & strMonth & strDay & strHour & strMinute & "00.000000+***"
End Function

‘====================================================
‘ Function to RETURN a Connection to the SMS Provider
‘====================================================
Function ConnectToSMSProvider(ServerName)
   Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
   Set objSWbemServices = objSWbemLocator.ConnectServer(ServerName, "root\sms")
   Set ProviderLocation = objSWbemServices.InstancesOf("SMS_ProviderLocation")
   For Each Location In ProviderLocation
      If Location.ProviderForLocalSite = True Then
         Set objSWbemServices = objSWbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
         Set ConnectToSMSProvider = objSWbemServices
      End If
   Next
End Function

More information about creating a maintenance window: http://msdn.microsoft.com/en-us/library/cc146686.aspx
More information about deleting a maintenance window: http://msdn.microsoft.com/en-us/library/cc143140.aspx

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.