Your application can delete a collection in System Center 2012 Configuration Manager by using the SMS_Collection Server WMI Class and class properties.
Caution |
---|
Care should be exercised when deleting any System Center 2012 Configuration Manager object. |
Collections are closely tied to packages, programs, and advertisements. For more information, see Software Distribution Overview.
These examples require the following values:
- A Windows Management Instrumentation (WMI)
connection object.
- An existing collection ID.
The following code is an example of the subroutine call in Visual Basic:
Copy Code | |
---|---|
Call DeleteCollection(swbemServices,"ABC00010") |
The following code is an example of the method call in C#:
Copy Code | |
---|---|
DeleteCollection(WMIConnection,"ABC00010") |
To delete a collection
-
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
-
Get the specific collection instance by using the collection ID provided.
-
Delete the collection by using the delete method.
Example
The following example method deletes a collection.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Visual Basic Script | Copy Code |
---|---|
' Setup a connection to the local provider. Set swbemLocator = CreateObject("WbemScripting.SWbemLocator") Set swbemServices= swbemLocator.ConnectServer(".", "root\sms") Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation") For Each Location In providerLoc If location.ProviderForLocalSite = True Then Set swbemServices = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode) Exit For End If Next Call DeleteCollection(swbemServices,"ABC00010") Sub DeleteCollection(connection, collectionIDToDelete) ' Get the specific collection instance to delete. Set collectionToDelete = connection.Get("SMS_Collection.CollectionID='" & collectionIDToDelete & "'") ' Delete the collection. collectionToDelete.Delete_ ' Display change information. Wscript.Echo "Deleted collection: " & collectionIDToDelete End Sub |
C# | Copy Code |
---|---|
public void DeleteCollection(WqlConnectionManager connection, string collectionIDToDelete) { // Note: On delete, the provider cleans up the SMS_CollectionSettings and SMS_CollectToSubCollect objects. try { // Get the specific collection instance to delete. IResultObject collectionToDelete = connection.GetInstance(@"SMS_Collection.CollectionID='" + collectionIDToDelete + "'"); // Delete the collection. collectionToDelete.Delete(); // Output the ID of the deleted collection. Console.WriteLine("Deleted collection: " + collectionIDToDelete); } catch (SmsException ex) { Console.WriteLine("Failed to delete collection. Error: " + ex.Message); throw; } } |
The example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection |
|
A valid connection to the SMS Provider. |
collectionIDToDelete |
|
Unique auto-generated ID containing eight characters. For more information, see the CollectionID property of SMS_Collection Server WMI Class. |
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_Collection Server WMI ClassConcepts
Software Distribution PackagesSoftware 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