How to Delete a Maintenance Window for a Collection
Updated: November 1, 2013
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
You can delete maintenance window, in System Center 2012 R2 Configuration Manager, 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
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
Get an existing collection settings instance by using the collection ID provided.
Get the existing service window object by using the maintenance window ID provided.
Delete the existing maintenance window.
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.
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; } }
Compiling the Code
The C# example requires:
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
Robust Programming
For more information about error handling, see About Configuration Manager Errors.
.NET Framework Security
For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.
Software Distribution Maintenance Windows
Software Distribution Packages
Software Distribution Programs
Software Distribution Advertisements
Configuration Manager Collections
Configuration Manager Software Distribution
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_CollectionSettings Server WMI Class
SMS_ServiceWindow Server WMI Class


The example method has the following parameters:
Parameter
Type
Description
connection
Managed: WqlConnectionManager
A valid connection to the SMS Provider.
targetCollectionID
Managed: String
The ID of the collection.
serviceWindowID
Managed: String
The ID of the maintenance window to delete.