The following example shows how to create a package in Microsoft System Center Configuration Manager 2007 by using the SMS_Package Server WMI Class class and class properties.

To create a package

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

  2. Create the new package object using the SMS_Package Server WMI Class class.

  3. Populate the new package properties.

  4. Save the package.

Example

The following example method creates a new package and populates its properties for use in software distribution.

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

Visual Basic Script  Copy Code
Sub CreatePackage(connection, newPackageName, newPackageDescription, newPackageSourceFlag, newPackageSourcePath)

	' Create the new package object. 
	Set newPackage = connection.Get("SMS_Package").SpawnInstance_

	' Populate the new package properties.
	newPackage.Name = newPackageName
	newPackage.Description = newPackageDescription
	newPackage.PkgSourceFlag = newPackageSourceFlag
	newPackage.PkgSourcePath = newPackageSourcePath

	' Save the package.
	newPackage.Put_

	' Output the new package name.
	wscript.echo "Created package: "  & newPackageDescription

End Sub
C#  Copy Code
public void CreatePackage(WqlConnectionManager connection, string newPackageName, string newPackageDescription, int newPackageSourceFlag, string newPackageSourcePath)
{
	try
	{
		// Create new package object.
		IResultObject newPackage = connection.CreateInstance("SMS_Package");

		// Populate new package properties.
		newPackage["Name"].StringValue = newPackageName;
		newPackage["Description"].StringValue = newPackageDescription;
		newPackage["PkgSourceFlag"].IntegerValue = newPackageSourceFlag;
		newPackage["PkgSourcePath"].StringValue = newPackageSourcePath;

		// Save new package and new package properties.
		newPackage.Put();

		// Output new package name.
		Console.WriteLine("Created package" + newPackageName);
}

	catch (SmsException ex)
	{
		Console.WriteLine("Failed to create 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.

newPackageName

  • Managed: String

  • VBScript: String

The name of the new package.

newPackageDescription

  • Managed: String

  • VBScript: String

The description for the new package.

newPackageSourceFlag

  • Managed: Integer

  • VBScript: Integer

The package source.

newPackageSourcePath

  • Managed: String

  • VBScript: String

The path to the package source.

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


Send comments about this topic to Microsoft.