Book Image

PowerShell Troubleshooting Guide

By : Mike Shepard
Book Image

PowerShell Troubleshooting Guide

By: Mike Shepard

Overview of this book

Table of Contents (15 chapters)
PowerShell Troubleshooting Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using Set-PSDebug


We already met the Set-PSDebug cmdlet in Chapter 5, Proactive PowerShell, where we learned that the –Strict switch can be used to ensure that references to variables that haven't been assigned will cause an error. In the context of debugging, the Set-PSDebug cmdlet gives a very simple debugging experience at the command line using the –Trace parameter and the –Step switch. Let's use a simple script to illustrate this:

foreach ($i in 1..10){
    write-host $i
}

While there should be no confusion over what this script will do when executed, watch what happens when we run Set-PSDebug –Trace 1 and then run the script:

I've truncated the output, but it should be clear that the PowerShell engine is outputting debug messages (like we could with Write-Debug) for each line that is executed. The output shown is from the ISE, but the cmdlet works in the console as well, although the formatting is slightly different:

Changing the value of the trace parameter to 2 gives a more detailed...