如何修改程序支持的平台

应用程序可以通过获取 和 SMS_Program 类的特定实例SMS_Package,然后将 类SupportedOperatingSystemsSMS_OS_Details实例添加到 属性,在 Configuration Manager 中添加受支持的平台。

修改程序支持的平台

  1. 设置与 SMS 提供程序的连接。

  2. 使用 SMS_Package 类获取现有包对象。

  3. 使用 SMS_Program 类获取现有程序对象。

  4. 创建并填充 类的 SMS_OS_Details 实例。

  5. 将新 SMS_OS_Details 实例添加到 SupportedOperatingSystems 步骤 3) (程序对象的 属性。

示例

以下示例方法演示如何为程序添加受支持的平台。

注意

此示例的细微变化可能会更改与特定包关联的所有程序的属性值。 有关示例,请参阅 如何列出所有程序及其最大运行时值 代码示例。 但是,若要使用 和 ProgramName访问特定程序的更高效方法,PackageID请参阅如何修改程序属性代码示例。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

Sub ModifySupportedPlatformsForProgram(connection,          _
                                       existingPackageID,   _
                                       existingProgramName, _
                                       newMaxVersion,       _
                                       newMinVersion,       _
                                       newName,             _
                                       newPlatform)

' Define a constant with the hexadecimal value for RUN_ON_ANY_PLATFORM.
    Const wbemFlagReturnImmediately = 16
    Const wbemFlagForwardOnly = 32
    Const RUN_ON_ANY_PLATFORM = &H08000000
    Dim packageQuery
    Dim package
    Dim allProgramsForPackage
    Dim programQuery
    Dim program
    Dim programPath
    Dim checkPlatformValue
    Dim tempSupportedPlatform
    Dim tempSupportedPlatformsArray
    ' Build a query to get the specified package.
     packageQuery = "SMS_Package.PackageID='" & existingPackageID & "'"
    ' Run the query to get the package.
    Set package = connection.Get(packageQuery)
    ' Output package name and ID.
    WScript.Echo "Package ID:     "  & package.PackageID
    WScript.Echo "Package Name:   "  & package.Name
    ' Build a query to get the programs for the package.
    programQuery = "SELECT * FROM SMS_Program WHERE PackageID='" & existingPackageID & "'"
    ' Run the query to get the programs.
    Set allProgramsForPackage = connection.ExecQuery(programQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)
    'The query returns a collection of program objects that needs to be enumerated.
    For Each program In allProgramsForPackage
        If program.ProgramName = existingProgramName Then
            ' Get all program object properties (in this case we specifically need some lazy properties).
            programPath = program.Put_
            programPath = Mid(programPath, InStr(1, programPath, ":") + 1)
            Set program = connection.Get(programPath)
            ' Check whether RUN_ON_ANY_PLATFORM is set.
            checkPlatformValue = (program.ProgramFlags AND RUN_ON_ANY_PLATFORM)
            If checkPlatformValue <> 0 Then
               ' RUN_ON_ANY_PLATFORM is set. Removing RUN_ON_ANY_PLATFORM value.
                program.ProgramFlags = (program.ProgramFlags XOR RUN_ON_ANY_PLATFORM)
            End If
            ' Output the program name that is being checked for supported platforms.
            WScript.Echo "Program Name: "  & program.ProgramName
            ' Create
            Set tempSupportedPlatform = connection.Get("SMS_OS_Details").SpawnInstance_
            ' Populate tempSupportedPlatform values.
            tempSupportedPlatform.MaxVersion = newMaxVersion
            tempSupportedPlatform.MinVersion = newMinVersion
            tempSupportedPlatform.Name       = newName
            tempSupportedPlatform.Platform   = newPlatform
            ' Get the array of supported operating systems.
            tempSupportedPlatformsArray = program.SupportedOperatingSystems
            ' Add the new supported platform values (object) to the temporary array.
            ReDim Preserve tempSupportedPlatformsArray (Ubound(tempSupportedPlatformsArray) + 1)
            Set tempSupportedPlatformsArray(Ubound(tempSupportedPlatformsArray)) = tempSupportedPlatform
            ' Replace the SupportedOperatingSystems object array with the new updated array.
            program.SupportedOperatingSystems = tempSupportedPlatformsArray
            ' Save the program.
            program.Put_
            ' Output success message.
            WScript.Echo "Supported Platforms Updated "
        End If
    Next
End Sub

public void ModifyProgramSupportedPlatforms(WqlConnectionManager connection,
                                    string existingPackageID,
                                    string existingProgramNameToModify,
                                    string newMaxVersion,
                                    string newMinVersion,
                                    string newName,
                                    string newPlatform)
{
    try
    {
        // Define a constant with the hexadecimal value for RUN_ON_ANY_PLATFORM.
        const Int32 RUN_ON_ANY_PLATFORM = 0x08000000;

        // 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)
            {
                // Get all properties, specifically the lazy properties, for the program object.
                program.Get();

                // Check whether RUN_ON_ANY_PLATFORM is already set.
                Int32 checkPlatformValue = (program["ProgramFlags"].IntegerValue & RUN_ON_ANY_PLATFORM);

                if (checkPlatformValue != 0)
                {
                    // RUN_ON_ANY_PLATFORM is set. Removing RUN_ON_ANY_PLATFORM value.
                    program["ProgramFlags"].IntegerValue = program["ProgramFlags"].IntegerValue ^ RUN_ON_ANY_PLATFORM;
                }

                // Create a new array list to hold the supported platform window objects.
                List<IResultObject> tempSupportedPlatformsArray = new List<IResultObject>();

                // Create and populate a temporary SMS_OS_Details object with the new operating system values.
                IResultObject tempSupportedPlatformsObject = connection.CreateEmbeddedObjectInstance("SMS_OS_Details");

                // Populate temporary SMS_OS_Details object with the new supported platforms values.
                tempSupportedPlatformsObject["MaxVersion"].StringValue = newMaxVersion;
                tempSupportedPlatformsObject["MinVersion"].StringValue = newMinVersion;
                tempSupportedPlatformsObject["Name"].StringValue = newName;
                tempSupportedPlatformsObject["Platform"].StringValue = newPlatform;

                // Populate the local array list with the existing supported platform objects (type SMS_OS_Details).
                tempSupportedPlatformsArray = program.GetArrayItems("SupportedOperatingSystems");

                // Add the newly created service window object to the local array list.
                tempSupportedPlatformsArray.Add(tempSupportedPlatformsObject);

                // Replace the existing service window objects from the target collection with the temporary array that includes the new service window.
                program.SetArrayItems("SupportedOperatingSystems", tempSupportedPlatformsArray);

                // Save the new values in the collection settings instance associated with the Collection ID.
                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;
    }
}

示例方法具有以下参数:

参数 类型 说明
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
与 SMS 提供程序的有效连接。
existingPackageID -管理: String
- VBScript: String
现有包的包 ID。
existingProgramName -管理: String
- VBScript: String
现有程序的程序名称。
newMaxVersion -管理: String
- VBScript: String
支持的最大版本。
newMinVersionsion -管理: String
- VBScript: String
支持的最低版本。
newName -管理: String
- VBScript: String
修改的程序名称。
newPlatform -管理: String
- VBScript: String
新平台。

编译代码

C# 示例需要:

命名空间

System

System.Collections.Generic

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

mscorlib

可靠编程

有关错误处理的详细信息,请参阅关于Configuration Manager错误

另请参阅

软件分发概述