Share via


How to Delete a Maintenance Window for a Collection

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

You can delete maintenance window, in Microsoft System Center Configuration Manager 2007, by using the SMS_CollectionSettings Server WMI Class and SMS_ServiceWindow Server WMI Class classes and properties.

To delete a maintenance window for a collection

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Get an existing collection settings instance by using the collection ID provided.

  3. Get the existing service window object by using the maintenance window ID provided.

  4. Delete the existing maintenance window.

  5. Save the collection settings instance and properties.

Note

The example method includes additional steps, primarily to handle the overhead of dealing with the service window objects, which are stored as embedded objects in the collection settings instance.

Example

The following example method deletes a specific maintenance window instance for a collection.

Important

This assumes that the collection instance can modified. This might not be the case at child sites, where the collections are owned by the parent site or sites.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Sub DeleteMaintenanceWindowFromCollection(connection,            _
                                          targetCollectionID,    _
                                          targetServiceWindowID)
                                       
     ' Get the specific collection settings instance.
    Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings.CollectionID='" & targetCollectionID &"'" )
              
    ' Populate the local array list with the existing service window objects (from the target collection).
    tempMaintenanceWindowArray = collectionSettingsInstance.ServiceWindows 

    ' Create new working copy of the array, to resize and eventually copy elements to.
    tempWorkingArray = tempMaintenanceWindowArray
    
    ' Resize the working copy of the array an element smaller than the original.
    ReDim PRESERVE tempWorkingArray (Ubound(tempMaintenanceWindowArray))

    ' Create a counter to assist in addressing each array element.
    arrayCounter = 0
    
    ' Create a variable to help determine whether the maintenance windows exists.
    foundMaintenanceWindowToDelete = false
    
    ' Enumerate through the array to access each maintenance window object.
    For Each maintenanceWindow in tempMaintenanceWindowArray
    
         ' If the target service window ID matches the one passed into the function, do not add it to the new array.
         ' The maintenance windows that do not match the one passed into the function are added to the working array.
         If maintenanceWindow.ServiceWindowID = targetServiceWindowID Then        
         
            foundMaintenanceWindowToDelete = true
            
         Else     
                                             
            ' Add the service window object to the temporary array / array element.
            Set tempWorkingArray(arrayCounter) = maintenanceWindow 
          
            ' Increment a counter for adding maintenance window objects to individual array elements.
            arrayCounter = arrayCounter + 1  
        
         End If
         
    Next
  
    ' If the target service window ID was found, then finish processing and save the collection settings instance.
    If foundMaintenanceWindowToDelete = true Then
    
        ' Replace the existing service window array from the target collection with the working array that does not includes the service window being deleted.
        collectionSettingsInstance.ServiceWindows = tempWorkingArray
        
        ' Save the new values in the collection settings instance associated with the collection ID.
        collectionSettingsInstance.Put_
              
        ' Output success message.
        wscript.echo "Maintenance Window " & targetServiceWindowID & " deleted."

    Else
    
        ' Output failure message.
        wscript.echo "Maintenance Window " & targetServiceWindowID & " not found."

    End If    
    
End Sub
public void DeleteMaintenanceWindowfromCollection(WqlConnectionManager connection, 
                                                  string targetCollectionID, 
                                                  string serviceWindowID)
{
    try
    {
        // Create a new array list to hold the service window objects.
        List<IResultObject> tempMaintenanceWindowArray = new List<IResultObject>();

        // Establish connection to collection settings instance associated with the target collection ID.
        IResultObject collectionSettings = connection.GetInstance(@"SMS_CollectionSettings.CollectionID='" + targetCollectionID + "'");

        // Populate the array list with the existing service window objects (from the target collection).
        tempMaintenanceWindowArray = collectionSettings.GetArrayItems("ServiceWindows");

        // Enumerate through the array list to access each maintenance window object.
        foreach (IResultObject maintenanceWindow in tempMaintenanceWindowArray)
        {
            // If the maintenance window ID matches the one passed in to the function, delete the maintenance window.
            if (maintenanceWindow["ServiceWindowID"].StringValue == serviceWindowID)
            {
                tempMaintenanceWindowArray.Remove(maintenanceWindow);
                Console.WriteLine("Deleted:");
                Console.WriteLine("Maintenance Window Name: " + maintenanceWindow["Name"].StringValue);
                Console.WriteLine("Maintenance Windows Service Window ID: " + maintenanceWindow["ServiceWindowID"].StringValue);
                break;
            }
        }

        // Replace the existing service window objects from the target collection with the temporary array that includes the new maintenance window.
        collectionSettings.SetArrayItems("ServiceWindows", tempMaintenanceWindowArray);

        // Save the new values in the collection settings instance associated with the Collection ID.
        collectionSettings.Put();
    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed. Error: " + ex.InnerException.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter Type Description

connection

swebemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

targetCollectionID

  • Managed: String

  • VBScript: String

The ID of the collection.

serviceWindowID

  • Managed: String

  • VBScript: String

The ID of the maintenance window to delete.

Compiling the Code

The C# example requires:

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

Security

For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.

See Also

Reference

SMS_CollectionSettings Server WMI Class

Concepts

Software Distribution Maintenance Windows
Configuration Manager Software Distribution
Software Distribution Packages
Software Distribution Programs
Software Distribution Advertisements
Configuration Manager Collections
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using WMI
SMS_ServiceWindow Server WMI Class