The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

PowerShell is King – Using PowerShell in a Task Sequence – Part 1

Posted by Mikael Nystrom on April 24, 2013

Traditionally VBscript has been “the” script type to use in OS Deployment scenarios, but with WinPE 4.0 in the ADK and MDT 2012 (u1) it is now an option in some scenarios.

Scenario:

You would like to use a PowerShell script that uses properties from the ZTIGather process

Solution:

Create the script and save it in the scripts folder in the deployment share (LTI) or in the MDT packages folder (ZTI) and then add it to the Task Sequence and if needed add any parameters you need to pass to the script. You don’t need to add any parameters from the LTI/ZTI environment, since they will be available within $TSenv:

image

The script:

Here is just a demo script that does not do anything at all, but it should give you an idea of how to create those scripts.


#Demo.ps1

#Setting
$ScriptFile = $MyInvocation.MyCommand.Name
$ScriptLocation  = Split-Path $MyInvocation.MyCommand.Path -Parent

#Setting Vars
$OSDComputername = $TSEnv:OSDCOMPUTERNAME
$Make = $TSEnv:Make
$Model = $TSEnv:Model

#Performing Action
Write-Progress -Activity "Checking Hardware…" -Status "Running Inventory" -PercentComplete 12 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Running $ScriptFile" -PercentComplete 12 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Running $ScriptLocation" -PercentComplete 12 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Make is now $Make" -PercentComplete 32 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Model is now $MODEL" -PercentComplete 85 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Waiting when we are at 99% just because we can…" -PercentComplete 99 -Id 1
Start-Sleep -Seconds 3

Write-Progress -Activity "Checking Hardware…" -Status "Done" -PercentComplete 100 -Id 1
Start-Sleep -Seconds 3


 

Explaining the script:

The first block is just to set variables for the script it self, it s possible to use other methods, but I prefer to use the built-in method in PowerShell to find out where I am instead of using the translated way in LTI/ZTI

The next block is to show you how to convert a variable from the Task Sequence using the $TSEnv: drive. You don’t need to do this, it is possible to use $TSEnv:OSDComputername directly, but I think this is a bit easier.

Remaining blocks will display the information on the screen while running.

Download Script

/mike

4 Responses to “PowerShell is King – Using PowerShell in a Task Sequence – Part 1”

  1. Thx Mike! Will continue to use $TSenv. great tips!

  2. Great Post! Keep up the Great Work!!!

  3. Hi Mike. Was just wondering about leaving out “Import-Module .\ZTIUtility.psm1” as the (very basic) MDT PowerShell documentation says in the prerequisites for creating MDT PowerShell scripts, is it no longer needed?

    • It depends on how you run it, if you use the run pwershell script in the TS, you dont need it, but if you use powershell.exe as command or when you test the script outside the TS, well then you need it.

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

 
%d bloggers like this: