Error Handling: Part3 – The Power of Set-PSDebug
From a scripting perspective this CMDLet is one of the greatest tools you could possibly have access to. The guys over at the Powershell Podcast also did a quick overview in Latest Podcast at http://powerscripting.net
Lets start with an overview of the CMDLet. A good portion of these will be covered in the help file (PS> get-help Set-PSDebug -full) So I highly recommed reviewing that documentation as well.
-trace
0 – turn off tracing
1 – trace script lines as they are executed
2 – trace basically everything
-step: Will prompt you for action
[Y] Yes (Default): Will Process the line and prompt again at the next.
[A] Yes to All: Will process all lines, Usually used after you Caught what you were looking for.
[N] No: Will STOP processing on all lines, effectively terminating the script.
[L] No to All: Will STOP processing on all lines, effectively terminating the script.
[S] Suspend: This option is awesome. This will open another runspace where you can inspect the
environment from the shell. Basically, if you want to figure out what the state of a variable or object
is at a given time. You can just Suspend and get the information or change it.
Type exit goes to the next prompt.
Continue with this operation?
1+ C:\tools\Testme.ps1 hello
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG: 1+ C:\tools\Testme.ps1 hello
Continue with this operation?
2+ $myhi = $hi
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG: 2+ $myhi = $hi
Continue with this operation?
3+ Write-Host "$Myhi was passed" -fore Green
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG: 3+ Write-Host "$Myhi was passed" -fore Green
hello was passed
Here are some gotcha’s for Stepping
- “[A] Yes to All” effectively turns off stepping
- “[N] No” and “[L] No to All” both do the same thing… Kill the processing.
- -step Automatically sets debug trace to level 1
-off: Turns off Debugging
-strict: Variable must be assigned a value before being referenced (Kind of like Option Explict in Vbscript.)
$myvar # This line will throw an error
$myvar = "Hi There"
$myvar # This line will output "Hi There"
What does the Debug output look like?
Here is the script we will be debugging
function foo{
Param($iFoo)
Return "$iFoo was Passed"
}
$myvar = foo $hi
Write-Host $myvar
Here is the script with Set-PSDebug -Trace 1
PS: C:\tools\Testme.ps1 Hi
DEBUG: 1+ C:\tools\Testme.ps1 Hi
DEBUG: 2+ function foo{
DEBUG: 6+ $myvar = foo $hi
DEBUG: 4+ Return "$iFoo was Passed"
DEBUG: 7+ Write-Host $myvar
Hi was Passed
Things to Notice
- Line numbers are referenced in the order they were called
- The Debug info is Prefaced with “DEBUG:”
- Stepping is not used by default
Here is the script with Set-PSDebug -Trace 2
PS: C:\tools\Testme.ps1 Hi
DEBUG: 1+ C:\tools\Testme.ps1 Hi
DEBUG: ! CALL script ‘Testme.ps1′
DEBUG: 2+ function foo{
DEBUG: 6+ $myvar = foo $hi
DEBUG: ! CALL function ‘foo’ (defined in file ‘C:\tools\Testme.ps1′)
DEBUG: 4+ Return "$iFoo was Passed"
DEBUG: ! SET $myvar = ‘Hi was Passed’.
DEBUG: 7+ Write-Host $myvar
Hi was Passed
Things to Notice
- You Now see calls to scripts, functions and variable assignments
- Calls to Scripts, Functions, and Setting Variable are prefaced with !
- Calls to function tell you where they came from
Summary:
I hope this post does justice to Set-PSDebug. It is extremely useful and powerful for writing scripts. I truly hope you take some time to play with this CMDLet and see where it takes you. It has done wonders for me.
Bonus:
Wanna see how the evironment effects a CMDLet or even just a little peak under the hood? Use Set-PSDebug -trace 2
DEBUG: 1+ Set-PSDebug -trace 2
70# Get-ChildItem c:\fes
DEBUG: 1+ Get-ChildItem c:\fes
DEBUG: 2+ if ($ErrorView -ne "CategoryView") {
DEBUG: 3+ $myinv = $_.InvocationInfo
DEBUG: ! SET $myinv = ‘System.Management.Automation.InvocationInfo’.
DEBUG: 4+ switch -regex ($myinv.MyCommand.CommandType)
DEBUG: 24+ if ($myinv.MyCommand.Name)
DEBUG: 26+ $myinv.MyCommand.Name + " : "; break;
DEBUG: 26+ $myinv.MyCommand.Name + " : "; break;
DEBUG: 2+ if ($_.InvocationInfo) {
DEBUG: 3+ $posmsg = $_.InvocationInfo.PositionMessage
DEBUG: ! SET $posmsg = ‘
At line:1 char:14
+ Get-ChildItem <<<< c:\fes’.
DEBUG: 7+ if ($ErrorView -eq "CategoryView") {
DEBUG: 11+ $_.Exception.Message + $posmsg
Get-ChildItem : Cannot find path ‘C:\fes’ because it does not exist.
At line:1 char:14
+ Get-ChildItem <<<< c:\fes
tshell :: Oct.16.2007 :: All, ErrorHandling, HowTo, Powershell, Scripting :: 3 Comments »
3 Responses to “Error Handling: Part3 – The Power of Set-PSDebug”
Leave a Reply
You must be logged in to post a comment.


[...] – The Power of Set-PSDebug New!Error Handling: Part2 – Throw "A word about Trapping" NewError Handling: Part1 – $Error Objects NewCitrix Load Evaluators Citrix functions Updated! Citrix, Citrix, and more Citrix! Test-Port(kinda like portqry without verbose output) Run-Command.ps1 : Run External Commands with Power! Import-ADUser: All I can say is WoW! Posh 55 / Vbscript 210 Backup-EventLog WordPress sidebar #wordpress_sidebar div.sidetitle{ padding: 1em 0 0 0; } Categories [...]
Very useful, compact. My summary would be: type ‘set-psdebug -trace 1′ then press ‘a’.
)
I am still wondering how to suspend execution (even if I have to put something special at the point I want to break at
I dont think you can have the option to suspend unless you keep the stepping. In v2 you can have break-points, but until this we have to step till we get there