分享方式:


如何建立程式

下列範例示範如何使用 SMS_Program 類別和類別屬性,在Configuration Manager中建立程式。

重要事項

當用戶端電腦上定義的維護期間設定為小於該程式的 [允許的執行時間上限] 設定時,任何已公告的程式將無法執行。 如需詳細資訊,請參閱 Configuration Manager 檔中的使用維護時段的程式執行案例。

若要建立程式

  1. 設定與 SMS 提供者的連線。

  2. 使用 SMS_Program 類別建立新的程式物件。

  3. 填入新的程式屬性。

    提示

    當您建立工作順序或虛擬應用程式封裝的程式時,SMS_Program屬性必須設定為特定值。 下表概述這些設定應設定的內容。

    工作順序

    內容名稱 屬性值
    ProgramName *

    虛擬應用程式套件

    內容名稱 屬性值
    CommandLine PkgGUID={E742FFD6-D539-42CC-9827-73535FC81E06}:VersionGUID={19366289-8C55-44E2-A5EC-7B385EFB4C30}

    注意: GUID 值取自虛擬應用程式的 XML 資訊清單檔案。
    ProgramName [虛擬應用程式]
  4. 儲存新的程式和屬性。

範例

下列範例方法會建立新的程式,並填入其屬性以用於軟體發佈。

如需呼叫範例程式碼的相關資訊,請參閱呼叫Configuration Manager程式碼片段


Sub CreateProgram(connection, existingPackageID, newProgramName, newProgramComment, newProgramCommandLine, newMaxRunTime)  

    ' Create the new program object.    Dim newProgram  
    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  
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;  
    }  
}  

範例方法具有下列參數:

參數 Type 描述
connection

swebemServices
-管理: WqlConnectionManager
- VBScript: SWbemServices
SMS 提供者的有效連線。
existingPackageID -管理: String
- VBScript: String
與程式相關聯的封裝名稱。
newProgramName -管理: String
- VBScript: String
新程式的名稱。
newProgramComment -管理: String
- VBScript: String
描述 Configuration Manager 主控台中程式的批註。
newProgramCommandLine -管理: String
- VBScript: String
啟動程式時執行的命令列。
newMaxRunTime -管理: Integer
- VBScript: Integer
用戶端電腦上程式執行的大約持續時間,以分鐘為單位。 此參數的最大值可以是 720 分鐘或 12 小時。

正在編譯程式碼

C# 範例需要:

命名空間

系統

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

mscorlib

健全的程式設計

如需錯誤處理的詳細資訊,請參閱關於Configuration Manager錯誤

另請參閱

軟體發佈概觀