How to List All Programs and Their Maximum Run Time Value

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

In Microsoft System Center Configuration Manager 2007, you can list all programs with their maximum run time values by using the SMS_Package Server WMI Class and SMS_Program Server WMI Class classes and class properties.

To list all programs and their maximum run times

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

  2. Load the available packages by using the SMS_Package class.

  3. Enumerate through each set of programs using the SMS_Program class and the PackageID property from each package.

  4. Output the package name, program name, and maximum run time value for each program.

Example

The following example method shows how to list all programs, with corresponding package name, program name, and maximum run times.

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

Sub ListPackagesProgramsandMaximumRunTimeValue(connection)

    ' Build query to get all of the packages. 
    packageQuery = "SELECT * FROM SMS_Package"
    
    ' Run query.
    Set allPackages = connection.ExecQuery(packageQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection of package objects that needs to be enumerated.
    For Each package In allPackages     
            
             ' Output package name and get the PackageID value to use in program query.
             wscript.echo ""
             wscript.echo "Package: "  & package.Name
             packageID = package.PackageID
            
             ' Build query to get the programs for the package. 
             packageQuery = "SELECT * FROM SMS_Program WHERE PackageID='" & packageID & "'"
    
            ' Run query.
            Set programsForPackage = connection.ExecQuery(packageQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

            ' The query returns a collection of program objects that needs to be enumerated.
            For Each program In programsForPackage     
             
               ' Output Maximum Runtime Value for each program found.
               wscript.echo "  Program: "  & program.ProgramName
               wscript.echo "  Maximum Runtime Value: "  & program.Duration
                          
           Next
                           
    Next

End Sub
public void ListPackagesProgramsandMaximumRunTimeValue(WqlConnectionManager connection)
{    
    try
    {
        // Build query to get the packages. 
        string packageQuery = "SELECT * FROM SMS_Package";

        // Load the specific program to change (programname is a key value and must be unique).
        IResultObject allPackages = connection.QueryProcessor.ExecuteQuery(packageQuery);

        // The query returns a collection of packages that needs to be enumerated.
        foreach(IResultObject package in allPackages)     
        {      
            // Output package name and get the PackageID value to use in program query.
            Console.WriteLine();
            Console.WriteLine("Package: "  + package["Name"].StringValue);
            string packageID = package["PackageID"].StringValue;
                      
            // Build query to get the programs for the package. 
            string programQuery = "SELECT * FROM SMS_Program WHERE PackageID='" + packageID + "'";

            // Load the all programs belonging to the package.
            IResultObject programsForPackage = connection.QueryProcessor.ExecuteQuery(programQuery);
            
            // The query returns a collection of programs that needs to be enumerated.
            foreach(IResultObject program in programsForPackage)     
            {      
                // Output Maximum Runtime Value for each program found.
                Console.WriteLine("   Program: "  + program["ProgramName"].StringValue);
                Console.WriteLine("   Maximum Runtime Value: "  + program["Duration"].IntegerValue);
            }               
        }
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to list the packages and programs. Error: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter

Type

Description

connection

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

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