How to Add a Condition to an Operating System Deployment Task Sequence Step
Updated: November 1, 2013
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
Conditions can be added to an operating system deployment step (action and group), in System Center 2012 R2 Configuration Manager, by creating a SMS_TaskSequence_Condition class instance and then associating it with the step. If the condition operands are all met, then the step is processed; otherwise it is not. The condition can have one or more operands that are instances of SMS_TaskSequence_Condition derived classes. You specify operators for the operands with instances of SMS_TaskSequence_ConditionOperator.
Note |
|---|
System Center 2012 R2 Configuration Manager provides several classes that provide useful expressions such as Windows Management Instrumentation (WMI) queries and file operations. For more information, see Operating System Deployment Server WMI Classes. |
To add a condition to a step
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
Obtain a task sequence step object. This can be an SMS_TaskSequence_Group object for a group, or a SMS_TaskSequenceAction derived class object for an action, for more information, see How to Add an Operating System Deployment Task Sequence Action.
Create a new condition by creating an instance of SMS_TaskSequence_Condition.
Create an expression for the condition by creating an instance of an SMS_TaskSequence_ConditionExpression derived class. For example, SMS_TaskSequence_RegistryConditionExpression.
Populate the expression properties.
Add the expression to the condition Operands property.
Add the condition to the task sequence step class Condition property.
Example
The following example method adds a condition to a supplied step that determines if the HKEY_LOCAL_MACHINE\MICROSOFT registry key exists. The SMS_TaskSequenc_RegistryCondition Expression is used to specify the condition.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Sub AddRegistryCondition (connection, taskSequenceStep)
Dim condition
Dim registryExpression
Dim operands
' Get or create the condition.
if IsNull ( taskSequenceStep.Condition) Then
Set condition = connection.Get("SMS_TaskSequence_Condition").SpawnInstance_
Else
Set condition = taskSequenceStep.Condition
End If
' Populate the condition.
Set registryExpression=connection.Get("SMS_TaskSequence_RegistryConditionExpression").SpawnInstance_
registryExpression.KeyPath="HKEY_LOCAL_MACHINE\MICROSOFT"
registryExpression.Operator="exists"
registryExpression.Type="REG_SZ"
registryExpression.Data=Null
' Add the condition.
operands=Array(registryExpression)
condition.Operands=operands
taskSequenceStep.Condition=condition
End Sub
public void AddRegistryCondition( WqlConnectionManager connection, IResultObject taskSequenceStep) { try { IResultObject condition; if (taskSequenceStep["Condition"].ObjectValue == null) { // Create a new condition. condition = connection.CreateEmbeddedObjectInstance("SMS_TaskSequence_Condition"); } else { // Get the existing condition. condition = taskSequenceStep.GetSingleItem("Condition"); } // Create and populate the expression. IResultObject registryExpression = connection.CreateEmbeddedObjectInstance("SMS_TaskSequence_RegistryConditionExpression"); registryExpression["KeyPath"].StringValue = @"HKEY_LOCAL_MACHINE\MICROSOFT"; registryExpression["Operator"].StringValue = "exists"; registryExpression["Type"].StringValue = "REG_SZ"; registryExpression["Data"].StringValue = null; // Get the operands and add the expression. List<IResultObject> operands = condition.GetArrayItems("Operands"); operands.Add(registryExpression); // Add the expresssion to the list of operands. condition.SetArrayItems("Operands", operands); // Add the condition to the sequence. taskSequenceStep.SetSingleItem("Condition", condition); } catch (SmsException e) { Console.WriteLine("Failed to create Task Sequence: " + e.Message); throw; } }
Compiling the Code
The C# example has the following compilation requirements:
System
System.Collections.Generic
System.Text
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
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 Securing Configuration Manager Applications.
Configuration Manager Operating System Deployment
Configuration Manager Objects
Configuration Manager Programming Fundamentals
How to Add an Operating System Deployment Task Sequence Action
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
Operating System Deployment Task Sequencing

The example method has the following parameters:
Parameter
Type
Description
connection
Managed: WqlConnectionManager
VBScript: SWbemServices
A valid connection to the SMS Provider.
taskSequenceStep
Managed: IResultObject
VBScript: SWbemObject
A valid task sequence step (SMS_TaskSequenceStep).