Freigeben über


How to Change the Maximum Run Time for a Program

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

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

Important

Any advertised program fails to run when the maintenance windows defined on the client computer are set for a period that is less than that program's Maximum allowed run time setting. See the topic "Program Run Scenario Using Maintenance Windows" in the Configuration Manager 2007 documentation for more information.

To change the maximum run time for a program

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

  2. Query for the programs associated with the existing package ID provided.

  3. Enumerate through the programs until a match for the program name is found.

  4. Replace the program maximum run time property with the one passed into the method.

  5. Save the program object and properties.

Example

The following example method changes the maximum run time for an existing program.

Note

A slight variation of this example could change property values for all of the programs associated with a specific package. For an example, see the How to List All Programs and Their Maximum Run Time Value code example. However, for a more efficient method of accessing a specific program, using the PackageID and ProgramName, see the How to Modify Program Properties code example.

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

Sub ModifyProgram(connection, existingpackageID, existingProgramNameToModify, newMaxRunTime)
    
    ' Build a query to get the programs for the package. 
    query = "SELECT * FROM SMS_Program WHERE PackageID='" & existingPackageID & "'"
    
    ' Run the query.
    Set programsForPackage = connection.ExecQuery(query, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    For Each program In programsForPackage     
             
        ' If a match for the program name is found, make the change(s).
        If program.ProgramName=existingProgramNameToModify Then
        
             ' Replace the existing package property (in this case the package description).
             program.Duration = newMaxRunTime
             
             ' Save the program with the modified properties.
             program.Put_
    
             ' Output program name.
             wscript.echo "Modified program: "  & program.ProgramName
             
             Exit For
        End If
    Next

End Sub
public void ModifyProgram(WqlConnectionManager connection, string existingPackageID, string existingProgramNameToModify, int newMaxRunTime)
{
    
    try
    {
        // Build query to get the programs for the package.
        string query = "SELECT * FROM SMS_Program WHERE PackageID='" + existingPackageID + "'";

        // Load the specific program to change (programname is a key value and must be unique).
        IResultObject programsForPackage = connection.QueryProcessor.ExecuteQuery(query);
        
        // The query returns a collection that needs to be enumerated.
        foreach(IResultObject program in programsForPackage)     
        {
            // If a match for the program name is found, make the change(s).
            if (program["ProgramName"].StringValue == existingProgramNameToModify)
            {                    
                 // Replace the existing package property (in this case the package description).
                 program["Duration"].IntegerValue = newMaxRunTime;
                 
                 // Save the program with the modified properties.
                 program.Put();
        
                 // Output program name.
                 Console.WriteLine("Modified program: "  + program["ProgramName"].StringValue);
            }
        }
                 
        
    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed to modify the program. Error: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter Type Description

connection

swbemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

existingPackageID

  • Managed: String

  • VBScript: String

The ID of an existing package with which to associate the program.

existingProgramNameToModify

  • Managed: String

  • VBScript: String

The name for the program to modify.

newMaxRunTime

  • Managed: Integer

  • VBScript: Integer

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

Compiling the Code

The C# example requires:

Namespaces

System

System.Collections.Generic

System.Text

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

Concepts

Configuration Manager Software Distribution
Software Distribution Packages
Software Distribution Programs
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using WMI
SMS_Package Server WMI Class
SMS_Program Server WMI Class