How to Set an Operating System Deployment Task Sequence Variable
In Configuration Manager, you create an operating system deployment task sequence variable by creating an instance of the SMS_TaskSequence_SetVariableAction class, adding to a task sequence. You can also create task sequence variables while the task sequence is running on the client. For more information, see How to Use Task Sequence Variables in a Running Configuration Manager Task Sequence.
A task sequence variable is a name/value pair that you can access by task sequence steps. You can also create computer and collection-specific variables. For more information, see How to Create a Collection Variable in Configuration Manager and How to Create a Computer Variable in Configuration Manager.
Note
Variables that are set with the SMS_TaskSequence_SetVariableAction class override variables that are set elsewhere. For example, if a collection variable and a SMS_TaskSequence_SetVariableAction have the same name, the value of the SMS_TaskSequence_SetVariableAction variable takes precedence.
To set a task sequence variable
Set up a connection to the SMS Provider. For more information, see SMS Provider fundamentals.
Get a task sequence to add the task sequence variable to. For more information, see How to Create an Operating System Deployment Task Sequence.
Create an instance of SMS_TaskSequence_SetVariableAction.
Set the VariableName and VariableValue properties for the variable that you are adding.
Add the SMS_TaskSequence_SetVariableAction object to the task sequence.
Example
The following example method sets a task sequence variable name and value.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Sub AddTaskSequenceVariable(connection, taskSequence, variableName, variableValue)
Dim variable
Dim steps
Set variable = connection.Get("SMS_TaskSequence_SetVariableAction").SpawnInstance_
variable.Name="MyTaskSequenceVariable"
variable.Description = "A task sequence variable"
variable.Enabled=True
variable.ContinueOnError=False
variable.VariableName=variableName
variable.VariableValue=variableValue
steps= Array(taskSequence.Steps)
ReDim steps (UBound (taskSequence.Steps)+1)
taskSequence.Steps(UBound(steps))=variable
End Sub
public void AddTaskSequenceVariable(
WqlConnectionManager connection,
IResultObject taskSequence,
string variableName,
string variableValue)
{
try
{
// Create the task sequence variable object.
IResultObject variable = connection.CreateEmbeddedObjectInstance("SMS_TaskSequence_SetVariableAction");
// Populate the properties.
variable["Name"].StringValue = "MyTaskSequenceVariable";
variable["ContinueOnError"].BooleanValue = false;
variable["Description"].StringValue = "A task sequence variable set with SMS_TaskSequence_SetVariableAction";
variable["Enabled"].BooleanValue = true;
variable["VariableName"].StringValue = variableName;
variable["VariableValue"].StringValue = variableValue;
// Add the step to the task sequence.
List<IResultObject> array = taskSequence.GetArrayItems("Steps");
array.Add(variable);
taskSequence.SetArrayItems("Steps", array);
}
catch (SmsException e)
{
Console.WriteLine("Failed to set task sequence variable: " + e.Message);
throw;
}
}
This example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection |
- Managed: WqlConnectionManager - VBScript: SWbemServices |
- A valid connection to the SMS Provider. |
taskSequence |
- Managed: WqlConnectionManager - VBScript: SWbemServices |
- The task sequence the variable is added to. |
variableName |
- Managed: String - VBScript: String |
The name of the variable. |
variableValue |
- Managed: String - VBScript: String |
The value for the variable. |
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 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
Task sequence overview
How to Use Task Sequence Variables in a Running Configuration Manager Task Sequence
How to Read a Task Sequence from a Task Sequence Package