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

Testings results with a test matrix

My Rename-Computer script is in production and I’ve made some major changes to it so it’s time for some major testing.  It’s important when testing your scripts to understand and document what your expected outcomes are. You should then run through each scenario and ensure that you get the desired results.

I’m running my script with its new changes on my technicians PC. Remember, I’ve neutralised the script and added debug lines. For reference, here’s the code again.

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{}

Below I’ve put together a simple testing matrix with the expected/desired results. Below which I’ve shown screenshots of the actual results.

Test 1 – Supply computer name Scriptimus to the command line
Expected results: Computer should be renamed.

Test 2 – Supply computer name Scriptimus and workgroup name ExMachina to the command line
Expected results: Computer and should be renamed and new workgroup joined.

Test 3 – Set Task Sequence Variable OSDComputername to CON-LAB1 and run script without supplying parameters
Expected results: Computer should be renamed.

Test 4 – Set Task Sequence Variables OSDComputername to CON-LAB1 and JoinWorkGroup to Continuum then run script without supplying parameters
Expected results: Computer and should be renamed and new workgroup joined.

Test 5 – Remove all variables and test script without parameters.
Expected results: No changes should be made.

Remove debug lines and reactivate script.

There’s a lot of debugging lines in this script that need to be removed. Sounds like a lot of work, right? No. Just comment out the line $DebugPreference=”Continue” and all the Write-Debug lines will be ignored as they are now run with the default “SilentlyContinue”.

The Import-Module ZTIUtility line can be left in as it will do nothing when run in production. In fact Microsoft suggest that this function will have helpful code in future MDT releases so they recommend that you keep the line in.

Finally the actual code needs to be re-enabled so remove the comment lines (#’s) from the 2 actual code lines and your script is now ready to return to production.

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{}
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