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

Adding new functions and features to a perfectly good script is common practice. One example is my evolving Rename-Computer script. I want to modify the way it works (again) but I don’t want to rename my computer and join a workgroup every time I test the script.

Using Write-Debug lines

One method I use when testing such a script is to neutralise it by replacing code with debug lines. I can then run the script and simulate the outcome. Here’s an example

Param (
 [String]$Name = $TSEnv:OSDComputername,
 [String]$WorkGroupName = $TSEnv:JoinWorkgroup
)
Begin{
$DebugPreference="Continue"
}
Process{
 # Rename the Computer using WMI
 # (Get-WmiObject win32_computersystem).rename($Name)
 Write-Debug "RUN COMMAND - Computer was renamed to $Name"
# Add the computer to a new workgroup
# Add-Computer -WorkGroupName $WorkGroupName -WarningAction SilentlyContinue
 Write-Debug "RUN COMMAND - Joined the Workgroup $WorkGroupName"
}
End{}

I’ve commented out the active commands and added Write-Debug commands below them. I’ve also activated the DebugPreference feature by adding the line $DebugPreference=”Continue” to the start of the script.

With this technique, I can test the logic of my code in a harmless environment while saving myself hours in reboots.

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.

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

  1. karthikeyan says:

    Hi Andrew,
    I want to add Network card and Raid Controller card drivers for the Dell Power Edge 2950,R610, M610 and R420 servers and i have downloaded the drivers from the dell Support site and the drivers are with .exe format and i could not inject this to MDT server and when try boot the server through PXE boot its giving blue screen error please suggest how do i add the drivers while deploying OS through MDT2012.

    Like

Leave a comment