How to Add an Operating System Deployment Task Sequence Action

An operating system deployment task sequence action is added to a task sequence, in Configuration Manager, by creating an instance of an SMS_TaskSequence_Action derived class and then adding it to the steps of the task sequence.

Note

Configuration Manager has a number of built-in actions that you can use. For example the command-line action class is SMS_TaskSequence_RunCommandLineAction. These classes derive from the SMS_TaskSequence_Action class.

SMS_TaskSequenceAction derives from the SMS_TaskSequence_Step class, which is the base class for both actions and groups. The task sequence stores its steps in an array of SMS_TaskSequence_Step, thus allowing actions and groups to be stored together.

To add a task sequence action

  1. Set up a connection to the SMS Provider. For more information see, SMS Provider fundamentals.

  2. Create a task sequence (SMS_TaskSequence) object. For more information, see How to Create an Operating System Deployment Task Sequence.

  3. Create an SMS_TaskSequenceAction derived class instance, for example, SMS_TaskSequence_RunCommandLineAction, for the action you want.

  4. Populate the action as appropriate.

  5. Add the action to the task sequences steps. This is stored the SMS_TaskSequence) class Steps property.

Example

The following example method creates a command-line action and adds it to the supplied task sequence.

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

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;  
    }  
}  

The example method has the following parameters:

Parameter Type Description
connection - Managed: WqlConnectionManager
- VBScript: SWbemServices
A valid connection to the SMS Provider.
taskSequence - Managed: IResultObject
- VBScript: SWbemObject
A valid task sequence.
Name - Managed: String
- VBScript: String
A name for the new action.
Description - Managed: String
- VBScript: String
A description for the action.

Compiling the Code

This C# example requires:

Namespaces

System

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

.NET Framework Security

For more information about securing Configuration Manager applications, see Configuration Manager role-based administration.

See Also

Objects overview How to Add a Condition to an Operating System Deployment Task Sequence Step
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
How to Create an Operating System Deployment Task Sequence Group
How to Delete an Operating System Deployment Task Sequence Action
Task sequence overview