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.
C# | Copy Code |
---|---|
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 |
|
A valid connection to the SMS Provider. |
targetCollectionID |
|
The ID of the collection. |
serviceWindowID |
|
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
Tasks
How to Connect to an SMS Provider in Configuration Manager by Using Managed CodeHow to Connect to an SMS Provider in Configuration Manager by Using WMI
Reference
SMS_CollectionSettings Server WMI ClassSMS_ServiceWindow Server WMI Class
Concepts
Software Distribution Maintenance WindowsSoftware Distribution Packages
Software Distribution Programs
Software Distribution Advertisements
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
Other Resources
Configuration Manager CollectionsConfiguration Manager Software Distribution