LTI/ZTI PowerShell: Using parameterised scripts in Task Sequences

There are methods in creating PowerShell scripts suitable for task sequence deployment. A good starting point is the Technet article about_Scripts. One of the most useful features of MDT/SCCM are the deployment properties. Today, I’m going to talk about using script parameters to pass deployment data into your scripts.

As a subject, I’m going to create a script that renames a computer then joins a workgroup. This is because MDT doesn’t do this natively(Post OS deployment). If you clone a VM then this is usually the first task you will do. Yes, PowerShell 3 has a cmdlet now for this but for earlier versions this is the method.

Here is the first example of a simple parameterised script that will work in a task sequence.

To accept parameters from the task sequence.

The code is very simple and uses a standard PowerShell construct:

Param (
 [String]$Name,
 [String]$WorkGroupName
)

Begin{}
Process{
 (Get-WmiObjectwin32_computersystem).rename($Name)
 Add-Computer -WorkGroupName $WorkGroupName -WarningAction SilentlyContinue
}
End{}

Save the script in the deployment share under the scripts folder and call it ZTIRename-Computer.ps1

Next, add it to MDT by creating a new Run PowerShell Script, task sequence step.

TS Step 1

Set the PowerShell script value as: %SCRIPTROOT%\ZTI-Rename-Computer.ps1

Then set the Parameters value as: -Name CON-LAB1 -WorkGroupName Continuum

This is how you accept the parameters from the task sequence itself.

Note: This particular script will need a restart but MDT can handle that itself so I added a Restart Computer step beneath it rather than code it in PowerShell.

To use deployment parameters with a script.

You will probably want to use your script on a number of different computers. In which case, you will need to call parameters from the customsettings.ini file or deployment database. First configure the computer name and workgroup parameters as normal in the customsettings.ini or database. eg:

OSDComputername = CON-Lab1
JoinWorkGroup = Continuum

Next, modify the Task Sequence step and change the Parameters section to these values:

-Name $TSEnv:OSDComputername -WorkGroupName $TSEnv:JoinWorkGroup

TS Step 2

This will use the deployment parameters and overwrite any defaults that may have been coded in the script. Speaking of which. . .

To code parameter defaults in the script.

In the script modify the Param section to store the values to the deployment properties. You can hard code the values or use the deployment parameters like in this example:

Param (
 [String]$Name = $tsenv:ComputerName,
 [String]$WorkGroupName = $tsenv:JoinWorkgroup
)

Begin{}
Process{
 (Get-WmiObjectwin32_computersystem).rename($Name)
 Add-Computer-WorkGroupName$WorkGroupName-WarningActionSilentlyContinue
}
End{}

For a script like this I would prefer to keep the parameters in the script to save having to enter the parameters in the task sequence.

Advertisements

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in MDT 2010, MDT 2012, SCCM, Scripting and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s