The following example shows how to configure the properties of an existing package, in System Center 2012 Configuration Manager, by using the SMS_Package class.
To configure an existing package
-
Set up a connection to the SMS Provider.
-
Load the existing package object by using SMS_Package class.
-
Populate any package properties (this example uses package description).
-
Save the package and the new package properties.
Example
The following example method configures package properties for software distribution.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Visual Basic Script | Copy Code |
---|---|
Sub ConfigurePackageProperties(connection, existingPackageID, newPackageDescription) ' Get the specific package object. Dim packageToConfigure Set packageToConfigure = connection.Get("SMS_Package.PackageID='" & existingPackageID & "'") ' Replace the existing package property (in this case the package description). packageToConfigure.Description = newPackageDescription ' Save the package with the modified properties. packageToConfigure.Put_ ' Output package ID and package name. wscript.echo "Configured Package " wscript.echo "Package ID: " & packageToConfigure.PackageID wscript.echo "Package Name: " & packageToConfigure.Name End Sub |
C# | Copy Code |
---|---|
public void ConfigurePackageProperties(WqlConnectionManager connection, string existingPackageID, string newPackageDescription) { try { // Get specific package instance to modify. IResultObject packageToConfigure = connection.GetInstance(@"SMS_Package.PackageID='" + existingPackageID + "'"); // Replace the existing package property with the new value (in this case the package description). packageToConfigure["Description"].StringValue = newPackageDescription; // Save package and modified package properties. packageToConfigure.Put(); // Output package ID and package name. Console.WriteLine("Configured Package "); Console.WriteLine("Package ID: " + packageToConfigure["PackageID"].StringValue); Console.WriteLine("Package Name: " + packageToConfigure["Name"].StringValue); } catch (SmsException ex) { Console.WriteLine("Failed to configure package. Error: " + ex.Message); throw; } } |
The example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection swbemServices |
|
A valid connection to the SMS Provider. |
existingPackageID |
|
The ID of the existing package. |
newPackageDescription |
|
The description for the new package. |
Compiling the Code
The C# example requires:
Namespaces
System
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
mscorlib
Robust Programming
For more information about error handling, see About Configuration Manager Errors.