如何添加操作系统部署任务序列操作

操作系统部署任务序列操作通过创建SMS_TaskSequence_Action派生类的实例,然后将其添加到任务序列的步骤(在 Configuration Manager 中)。

注意

Configuration Manager有许多可以使用的内置操作。 例如,命令行操作类 是SMS_TaskSequence_RunCommandLineAction。 这些类派生自 SMS_TaskSequence_Action 类。

SMS_TaskSequenceAction 派生自 SMS_TaskSequence_Step 类,类是操作和组的基类。 任务序列将其步骤存储在 SMS_TaskSequence_Step数组中,从而允许将操作和组存储在一起。

添加任务序列操作

  1. 设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识

  2. ) 对象 (SMS_TaskSequence 创建任务序列。 有关详细信息,请参阅 如何创建操作系统部署任务序列

  3. 为所需操作创建 SMS_TaskSequenceAction 派生类实例,例如 ,SMS_TaskSequence_RunCommandLineAction

  4. 根据需要填充操作。

  5. 将操作添加到任务序列步骤。 这是存储 SMS_TaskSequence) 类 Steps 属性。

示例

以下示例方法创建命令行操作并将其添加到提供的任务序列。

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

Sub AddTaskSequenceActionCommandLine(connection, taskSequence, name, description)     

    Dim steps  
    Dim action    

    Set action = connection.Get("SMS_TaskSequence_RunCommandLineAction").SpawnInstance_  

    action.CommandLine = "cmd /c Echo Hello"  
    action.Name=name  
    action.Description=description  
    action.Enabled=True  
    action.ContinueOnError=False  

      If IsNull(taskSequence.Steps) Then  
        steps = Array(action)  
        taskSequence.Steps=steps  
    Else  
        steps= Array(taskSequence.Steps)  
        ReDim steps (UBound (taskSequence.Steps)+1)   
        taskSequence.Steps(UBound(steps))=action  
    End if    

End Sub  

public IResultObject AddTaskSequenceActionCommandLine(  
    WqlConnectionManager connection,   
    IResultObject taskSequence,  
    string name,   
    string description)  
{  
    try  
    {  
        // Create the new step.  
        IResultObject ro;  

        ro = connection.CreateEmbeddedObjectInstance("SMS_TaskSequence_RunCommandLineAction");  
        ro["CommandLine"].StringValue = @"cmd /c Echo Hello";  

        ro["Name"].StringValue = name;  
        ro["Description"].StringValue = description;  
        ro["Enabled"].BooleanValue = true;  
        ro["ContinueOnError"].BooleanValue = false;  

        // Add the step to the task sequence.  
        List<IResultObject> array = taskSequence.GetArrayItems("Steps");  

        array.Add(ro);  

        taskSequence.SetArrayItems("Steps", array);  

        return ro;  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("Failed to add action: " + e.Message);  
        throw;  
    }  
}  

示例方法具有以下参数:

参数 类型 说明
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
与 SMS 提供程序的有效连接。
taskSequence -管理: IResultObject
- VBScript: SWbemObject
有效的任务序列。
Name -管理: String
- VBScript: String
新操作的名称。
Description -管理: String
- VBScript: String
操作的说明。

编译代码

此 C# 示例需要:

命名空间

System

System.Collections.Generic

System.Text

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

可靠编程

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

.NET Framework 安全性

有关保护Configuration Manager应用程序的详细信息,请参阅Configuration Manager基于角色的管理

另请参阅

对象概述如何向操作系统部署任务序列步骤添加条件
如何使用托管代码连接到 Configuration Manager 中的 SMS 提供程序
如何使用 WMI 连接到 Configuration Manager 中的短信提供程序
如何创建操作系统部署任务序列组
如何删除操作系统部署任务序列操作
任务序列概述