The following example shows how to set the distribute on demand flag property of an existing package by using the SMS_Package Server WMI Class class in System Center 2012 Configuration Manager.

To set the distribute on demand flag

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

  2. Load the existing package object by using SMS_Package Server WMI Class class.

  3. Populate the package flag property.

  4. Save the package and the new package properties.

Example

The following example method sets the distribute on demand flag for a package.

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

Visual Basic Script  Copy Code
Sub SetDistributeOnDemandFlag(connection, 	 _
							existingPackageID)

	' Define a constant with the hexadecimal value for the DISTRIBUTE_ON_DEMAND. 
	DISTRIBUTE_ON_DEMAND = &H40000000

	' Get the specific package instance to modify. 
	Set packageToModify = connection.Get("SMS_Package.PackageID='" & existingpackageID & "'")

	' List the existing property values.
	Wscript.Echo " "
	Wscript.Echo "Values before change: "
	Wscript.Echo "--------------------- "
	Wscript.Echo "Package Name:			" & packageToModify.Name
	Wscript.Echo "Package Flags (integer): " & packageToModify.PkgFlags

	' Set the new property value.
	packageToModify.PkgFlags = packageToModify.PkgFlags OR DISTRIBUTE_ON_DEMAND

	' Save the package.
	packageToModify.Put_ 

	' Output the new property values.
	Wscript.Echo " "
	Wscript.Echo "Values after change:  "
	Wscript.Echo "--------------------- "
	Wscript.Echo "Package Name:			 " & packageToModify.Name
	Wscript.Echo "Package Flags (integer):  " & packageToModify.PkgFlags

End Sub
C#  Copy Code
public void SetDistributeOnDemandFlag(WqlConnectionManager connection,
									string existingPackageID)
{
	// Define a constant with the hexadecimal value for DISTRIBUTE_ON_DEMAND. 
	const Int32 DISTRIBUTE_ON_DEMAND = 0x40000000;

	try
	{
		// Get the specific package instance to modify. 
		IResultObject packageToModify = connection.GetInstance(@"SMS_Package.PackageID='" + existingPackageID + "'");

		// List the existing property values.
		Console.WriteLine();
		Console.WriteLine("Values before change:");
		Console.WriteLine("_____________________");
		Console.WriteLine("Package Name:			" + packageToModify["Name"].StringValue);
		Console.WriteLine("Package Flags (integer): " + packageToModify["PkgFlags"].IntegerValue);

		// Modify the PkgFlags value to include the DISTRIBUTE_ON_DEMAND value.
		packageToModify["PkgFlags"].IntegerValue = packageToModify["PkgFlags"].IntegerValue | DISTRIBUTE_ON_DEMAND;

		// Save the package with the new value.
		packageToModify.Put();

		// Reload the package to verify the change.
		packageToModify.Get();

		// List the existing (modified) property values.
		Console.WriteLine();
		Console.WriteLine("Values after change:");
		Console.WriteLine("_____________________");
		Console.WriteLine("Package Name:			" + packageToModify["Name"].StringValue);
		Console.WriteLine("Package Flags (integer): " + packageToModify["PkgFlags"].IntegerValue);
}
	catch (SmsException ex)
	{
		Console.WriteLine("Failed to modify package. Error: " + ex.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.

existingPackageID

  • Managed: String

  • VBScript: String

The ID of the package.

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