LTI/ZTI PowerShell: Debugging Scripts (Part 2 of 3)

Testing variables with the ZTIUtility module

My Rename-Computer script worked great on a computer in a workgroup environment. But now, I have a new server that I want to rename before I join it to a domain. I’ve worked out that I’ll need the script to have the following functions:

  • Rename the Computer only if a parameter is passed to the script or if a value exists in the task sequence variable OSDComputername. If neither is true then do nothing.
  • Join a new Workgroup only if a parameter is passed to the script or if a value exists in the task sequence variable JoinWorkgroup. If neither is true then do nothing.

The first thing I need to do is create task sequence variables for testing outside of MDT. For this I will need the PowerShell drives TSEnv and TSEnvlist. Fortunately, there’s a module that can create these this for me. After which I can create the test variables manually.

To set this up, first copy the folders Microsoft.BDD.TaskSequenceModule and ZTIUtility from your deployment share \tools\modules folder to your Powershell modules folder.

Now you can add the line Import-Module ZTIUtility to your script and it will create your PSDrives for you.

In my case, I want to test that my script works with the OSDComputername set to CON-LAB1. From a PowerShell console I import the ZTIUtility module and set the variable. These 2 lines will do the trick.

Import-Module ZTIUtility
$TSEnv:OSDcomputername="CON-LAB1"

I can add/change a variable by using this example.

$TSEnv:OSDcomputername =”CON-LAB1″

I can call/test the variable in a number of ways. For example, by typing $TSEnv:OSDcomputername 

Next, after a moment in the script editor I’ve add my script changes, this is how my script looks now.

Param (
 [String]$Name = $TSEnv:OSDComputername,
 [String]$WorkGroupName
)

Begin{
$DebugPreference="Continue"
Import-Module ZTIUtility
}
Process{
If ($Name) {
 # Rename the Computer using WMI
 # (Get-WmiObject win32_computersystem).rename($Name)
 Write-Debug "RUN COMMAND - Computer was renamed to $Name"
 }
# If the WorkGroupName variable has no value passed to the script.
 If (!($WorkGroupName)) {
 Write-Debug "WorkGroupName variable has no value passed to the script."
# If the Task Sequence variable JoinWorkgroup has a value.
 If ($TSEnv:JoinWorkgroup) {
 Write-Debug "Task Sequence variable JoinWorkgroup has a value."
 [String]$WorkGroupName = $TSEnv:JoinWorkgroup
 }
 }

 # Join a Workgroup
 # If the WorkGroupName variable has a value.
 If ($WorkGroupName) {
 Write-Debug "WorkGroupName variable has a value."
 # Add-Computer -WorkGroupName $WorkGroupName -WarningAction SilentlyContinue
 Write-Debug "RUN COMMAND - Joined the Workgroup $WorkGroupName"
 }
}
End{}

In my next post, I walk through a testing matrix.

Advertisements

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in MDT 2010, MDT 2012, PowerShell, 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