This post should have been released four years ago, but often, life is more surprising that you’d expect ! Anyway, the wait is over, so:
Following my very old previous post for MDT, here is how to create a test environment to ease the development of your vbscript and Powershell scripts that run during a task sequence on SCCM. If you are not sure why you would need this tool, let’s just remind that:
- The test environment runs in the very same context as a task sequence, so now you know exactly what to expect when on an “x86 system account”!
- You have full and real time access to SCCM/MDT variables.
- You can get all those goodies under any of your favorite PowerShell IDE.
- Further details can be red on my first post.
Prerequisites
- A working SCCM CB Infrastructure
- MDT 8450 integrated to SCCM
- A “Developer” machine with the SCCM client installed on.
- the following files: CreateTestEnv-Profile.ps1 & ZTISuspend.wsf
How to install
Locate and browse to your MDT package source file and add CreateTestEnv-Profile.ps1& ZTISuspend.wsf under the Scripts folder. In the Config Manager console, right click on the package and select Update Distribution Points.
Next, go to Software Library>Operating Systems>Task Sequences, on the task bar click on Create MDT Task Sequence.
On the Create MDT Task sequence window, select the following parameters:
- Choose Template
- Microsoft Deployment Custom Task Sequence
- General
- Task sequence name: OSDC-DevTool (or what ever you want)
- Boot Image
- Specify an existing boot image: any boot image of your choice.
- MDT Package
- Specify an existing Microsoft Deployment Toolkit Files package: The MDT package you updated in the previous step
- Settings Package
- Specify an existing Settings package: Your regular MDT setting package
Once the task sequence is generated, right click on it and select edit we will make it looks and smell like this:
The fives first steps are from the original TS so no need to detail, simply rearrange to make them look like the screen capture.
If you are asking why should we keep the Install Applications group, well it’s just for convenience, one day or an other you’ll have to develop something for a package or an application so with that group in place you know that you are ready.
The more interesting part begins with the Copy PowerShell Module step
Command line: xcopy "%DeployRoot%\Tools\Modules""C:\Windows\System32\WindowsPowerShell\v1.0\Modules" /S /Y
The purpose of this step is to copy the PowerShell Modules from MDT to your local computer
Create then, a Test Env profile step
Command line: PowerShell.exe -ExecutionPolicy Bypass "%SCRIPTROOT%\CreateTestEnv-Profile.ps1" -Action create
Purpose: creating a local PowerShell profile that will launch the MDT module and create Tsenv: and TsenvList: PowerShell drives
Launch Test Environment step
Command Line:%ToolRoot%\serviceUI.exe -process:TSProgressUI.exe %SYSTEMROOT%\System32\wscript.exe %ScriptRoot%\ZTISuspend.wsf
Purpose: Launching PowerShell ISE (Or PowerGUI if present) with the task sequence drives TSenv: and TSenvList: loaded
Remove test env profile step
Command Line: PowerShell.exe -ExecutionPolicy Bypass "%SCRIPTROOT%\CreateTestEnv-Profile.ps1" -Action remove
Purpose: This step will only occurs once you’ve closed PowerShell ISE or PowerGUI, and will remove the custom PowerShell profile
Pretty simple to build, now let’s see how this works and how to use it.
How to use
So first, you need to login to your “Developer” machine where the Config Manager client is already installed. Open the software center and click on the OSDC-DevTool Task Sequence. Within few seconds the task sequence should launch:
And at some point Powershell ISE our PowerGUI should appears:
As shown in the above capture, the PowerShell editor is loaded with the MDT PowerShell module with the instant benefit of getting access to TSenv: and TSEnvList: Drives.
for an overview of the available properties, let’s start with Get-ChildItem Tsenv: | Out-GridView
Pretty cool ! isn’t it ?
Now let’s see how we can interact with those properties: let’s say that we must build a computer name with the SMS site code plus the serial number of the destination computer. Also the First letter will tell if the computer is a desktop, a laptop or a VM:
Result:
In this example, you’ve seen how to get values for properties ( If ($TSEnv:IsDesktop -eq $True) ). Note that you can cast out value directly like this : Write-host $TSEnv:IsDesktop
Creating or updating a property can be done using $TSEnv:MyProperty = MyNewValue.
We’ve also talked about TSEnvList: drive, this one stores properties list like IP or MAC address:
As you may not have recognized, TSEnvlist is array (not hash table):
And, like a few other things with PowerShell, it doesn’t behave exactly as you would expect… so creating and editing a list property can be done like this:
Explanations: property lists can’t be used directly, except if you define a fixed array like the one shown on line 1.
So if you start with a fixed array or with one of the already staged one, you should copy your $TSEnvList variable to a regular PowerShell variable. This is what we do on line 2.
To change a static array to a dynamic one, an Invoke method is used against our new variable on line 3.
Once we have a dynamic array, we can add or remove items like shown on lines 4 and 5
Finally, when all changes are made, you can commit to the property list variable like on line 6
Ok, so now you have the basic. This dev environment is really a life saver if you are serious about PowerShell and SCCM task sequences. Note that there is also a Command Prompt console that opens just before the powershell IDE. With this one you can try and test yours vbscript if you need to !
Happy Dev/Deploy !!