ps error handling and debugging
DESCRIPTION
TRANSCRIPT
PowerShell Error Handling and PowerShell Error Handling and DebuggingDebuggingDon JonesDon JonesSenior Partner & Principal TechnologistConcentrated Technology, LLC
This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it
within your own organization however you like.
For more information on our company, including information on private classes and upcoming conference appearances, please
visit our Web site, www.ConcentratedTech.com.
For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg
This work is copyright ©Concentrated Technology, LLC
CAUTION: DEMOS AHEADCAUTION: DEMOS AHEAD
This is a demonstration-intensive session (very few slides)
I will capture a shell transcript and save all of my scripts
You can download these (in a week or so) from ConcentratedTech.com (there’s a “Conference Materials” link in the menu)
3 • Don Jones • ConcentratedTech.com
AgendaAgenda
Debugging: Why and How– Trace Code– Breakpoints
Error Handling– How-to– How-not-to
4 • Don Jones • ConcentratedTech.com
DebuggingDebugging
Two types of bugs:– Typos. The shell usually gives you an error,
with a location, for these.– Logic. This means no errors, but you don’t
get the behavior you want.
5 • Don Jones • ConcentratedTech.com
Logic Errors: The CauseLogic Errors: The Cause
99% of the time, because some property or variable contained something other than you thought it did
The solution:– Have a clear expectation (write it down)– Debug the script to see where reality differs
from your expectation
If you don’t have an expectation – you can’t debug.
6 • Don Jones • ConcentratedTech.com
Technique #1: Trace CodeTechnique #1: Trace Code
Use Write-Debug Output variables when their contents
change Output objects (there’s a trick to
doing so) to examine properties Enable debug output with
$DebugPreference Leave trace code in – just disable
output when you’re not debugging
7 • Don Jones • ConcentratedTech.com
Technique #2: BreakpointsTechnique #2: Breakpoints
Set a breakpoint (Set-PSBreakpoint) When the script “breaks,” manually
examine properties, variables, etc. Run Exit to resume execution of the
script from “break mode” Set breakpoints on:
– Line or line/column of a script file– When a variable is read, written, or either– When a command is executed
8 • Don Jones • ConcentratedTech.com
Technique #3: Step DebuggerTechnique #3: Step Debugger
A bit outdated now that we have breakpoints (in PowerShell v2)
Enable using Set-PSDebug –step Read that cmdlet’s help file for more
information Typically, stick with breakpoints
9 • Don Jones • ConcentratedTech.com
Remember!Remember!
No expectation = No debugging You can only debug when you detect
a difference between expectations and reality
Let’s walk through an example
10 • Don Jones • ConcentratedTech.com
Error HandlingError Handling
“Catch” an error and do something about it
Most cmdlets generate “non-terminating” errors – e.g., the cmdlet still tries to keep running. You can’t “catch” these.
Use –ErrorAction (-EA) parameter to change them to “terminating” exceptions
Catch them with a Try…Catch…Finally block
Grab the error in –ErrorVariable (-EV) if desired
11 • Don Jones • ConcentratedTech.com
DO NOTDO NOT
Do not set $ErrorActionPreference globally within a script
Doing so suppresses errors – even the ones telling you about a typo or something
12 • Don Jones • ConcentratedTech.com
Option #2Option #2
You can also define a Trap block to catch any otherwise-uncaught terminating exceptions
Exit Trap with Continue or Break– Continue tries to resume execution following
the error-causing line– Break exits and passes the error to
whatever called the script
Harder to work with – deeply involves PowerShell scope, which can be complex to follow
13 • Don Jones • ConcentratedTech.com
Final ExamFinal Exam
Let’s build a script, debug it, and include error handling – all from scratch
14 • Don Jones • ConcentratedTech.com
Final Notes…Final Notes…
Please be sure to submit a session evaluation form!
Download slides & materials from www.ConcentratedTech.com within one week!
Blog, URLs, and other information is also available at www.ConcentratedTech.com for your reference
More resources at www.ShellHub.com Thank you very much!
15 • Don Jones • ConcentratedTech.com
This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it
within your own organization however you like.
For more information on our company, including information on private classes and upcoming conference appearances, please
visit our Web site, www.ConcentratedTech.com.
For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg
This work is copyright ©Concentrated Technology, LLC