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

Important
Any advertised program will fail to run when the maintenance windows that are defined on the client computer are set for a period that is less than that program's Maximum allowed run time setting. For more information, see the topic Program Run Scenario using Maintenance Windows in the Configuration Manager 2007 documentation.

To create a program

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

  2. Create the new program object using the SMS_Program Server WMI Class class.

  3. Populate the new program properties.

  4. Save the new program and properties.

Example

The following example method creates a new program 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 CreateProgram(connection, existingPackageID, newProgramName, newProgramComment, newProgramCommandLine, newMaxRunTime)

	' Create the new program object.
	Set newProgram = connection.Get("SMS_Program").SpawnInstance_

	' Populate the program properties.
	newProgram.PackageID = existingPackageID
	newProgram.ProgramName = newProgramName
	newProgram.Comment = newProgramComment
	newProgram.CommandLine = newProgramCommandLine
	newProgram.Duration = newMaxRunTime

	' Save the new program and properties.
	newProgram.Put_

	' Output new program name.
	wscript.echo "Created program: " & newProgramName

End Sub
C#  Copy Code
public void CreateProgram(WqlConnectionManager connection, 
						string existingPackageID, 
						string newProgramName, 
						string newProgramComment, 
						string newProgramCommandLine,
						int newMaxRunTime)
{
	try
	{
		// Create an instance of SMS_Program.
		IResultObject newProgram = connection.CreateInstance("SMS_Program");

		// Populate basic program values.
		newProgram["PackageID"].StringValue = existingPackageID;
		newProgram["ProgramName"].StringValue = newProgramName;
		newProgram["Comment"].StringValue = newProgramComment;
		newProgram["CommandLine"].StringValue = newProgramCommandLine;
		newProgram["Duration"].IntegerValue = newMaxRunTime;

		// Save the new program instance and values.
		newProgram.Put();

		Console.WriteLine("Created program: " + newProgramName);
}
	catch (SmsException ex)
	{
		Console.WriteLine("Failed to create program. 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 name of the package associated with the program.

newProgramName

  • Managed: String

  • VBScript: String

The name for the new program.

newProgramComment

  • Managed: String

  • VBScript: String

Comment that describes the program in the Configuration Manager console.

newProgramCommandLine

  • Managed: String

  • VBScript: String

The command line that runs when the program is launched.

newMaxRunTime

  • Managed: Integer

  • VBScript: Integer

The approximate duration, in minutes, of program execution on the client computer.

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.