PowerShell/SCCM - Create a Dynamic Variable list during the task sequence

In my previous article I talked about SCCM Application and how to retrieve the applications targeted to a user.

Today I'll show how we can build a function to create a list of dynamic variables. This would be used in a Task Sequence during the Operating System Deployment in the task "Install Application".

First we will need to find the list of applications and then create a variable with a specific pattern for each of them.

What does it look like inside the task Sequence ?

Before we dig into the PowerShell, I think it is important to understand where everything will goes.

In the following task "Run PowerShell Shell Script", the script will retrieve in SCCM all the Applications advertised on the users (showed in previous article) and then create a bunch of variables that will be used in the task "Install Application".

When you use the task "Install Application" with the option 'Install applications according to dynamic variable list', the task expect to find a bunch of variables that start by "Base Variable Name" specified below, here"FX".

So you should have variables such as:


Each of those variables value must contain the name of the Application you want to deploy.

Retrieving the application targeted to a user

Now let's focus on PowerShell. Assuming you have following list of application targeted to a user:

Creating a list of variable with a specific pattern

First we need to work on the variable incrementation, this can easily be done with a counter, the list of application and some Composite Formatting.

# Create a Counter
$Counter = 1

# Output the base variable with an incremented number
#  for each Application
$ApplicationList | ForEach-Object {
    # Using the Composite Formating {0:00}
    #  to add a two difit number
    "FX{0:00}" -f $Counter
    # Increment the counter

We are creating a counter to have our start number, 1 in this case. (it's important to follow a sequence without skipping any number, the task "Install Application" won't like it otherwise).

We then use $ApplicationList variable, which contains 16 Applications, to go through the pipeline 16 times and create a value with different number each time.

The weird looking string "FX{0:00}" is using something called Composite Formatting, this help us inject some information in a String with a specific format. There are two parts, separated by the colon. The first part represent the index and the second part the FormatString {index:formatString}

The format operator  -f places the composite formatting with the format item on the left side of the -f operator and the object list on the right side of the -f operator.

This also work with multiple values:

Creating Variable in the SCCM Task Sequence Environment

Finally... creating the Task Sequence Variable is pretty straight forward, here is an example.
$TaskSequenceEnvironment = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TaskSequenceEnvironment.value("VariableName") = "ApplicationName"
The class Microsoft.SMS.TSEnvironment however is only available inside the Task Sequence.
So we can implement this into the code by doing something like:
$TaskSequenceEnvironment = New-Object -COMObject Microsoft.SMS.TSEnvironment    
$Counter = 1

$ApplicationList | ForEach-Object {
    $Variable = "$BaseVariableName{0:00}" -f $Counter
    $TaskSequenceEnvironment.value("$Variable") = "$_"
    $Counter++| Out-Null

Download the full function

New-SCCMTSAppVariable (GitHub Repository)

More Information

Note also that there is a bunch of built-in variables available when you use Task Sequence, See here.
Another alternative is to create Collection Variables or Computer Variables and retrieve them during your Task Sequence.

Thanks for reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn. You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page.

No comments:

Post a Comment