PS error handling and debugging

907 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

PS error handling and debugging

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

×