SlideShare a Scribd company logo
PowerShell Error Handling and Debugging Don Jones Senior Partner & Principal Technologist Concentrated 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 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)  • Don Jones • ConcentratedTech.com
Agenda Debugging: Why and How Trace Code Breakpoints Error Handling How-to How-not-to  • Don Jones • ConcentratedTech.com
Debugging 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.  • Don Jones • ConcentratedTech.com
Logic 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.  • Don Jones • ConcentratedTech.com
Technique #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  • Don Jones • ConcentratedTech.com
Technique #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  • Don Jones • ConcentratedTech.com
Technique #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  • Don Jones • ConcentratedTech.com
Remember! No expectation = No debugging You can only debug when you detect a difference between expectations and reality Let ’s walk through an example  • Don Jones • ConcentratedTech.com
Error 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  • Don Jones • ConcentratedTech.com
DO NOT Do not set $ErrorActionPreference globally within a script Doing so  suppresses  errors – even the ones telling you about a typo or something  • Don Jones • ConcentratedTech.com
Option #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  • Don Jones • ConcentratedTech.com
Final Exam Let ’s build a script, debug it, and include error handling – all from scratch  • Don Jones • ConcentratedTech.com
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!  • 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

More Related Content

What's hot (20)

PPTX
PowerShell crashcourse for sharepoint
Concentrated Technology
 
PPT
Securely connecting to apps over the internet using rds
Concentrated Technology
 
PPTX
PowerShell custom properties
Concentrated Technology
 
PPTX
Virtualization auditing & security deck v1.0
Concentrated Technology
 
PPT
Rapidly deploying software
Concentrated Technology
 
PPT
Prepping software for w7 deployment
Concentrated Technology
 
PPTX
Vdi in-a-box
Concentrated Technology
 
PPT
Wsus sample scripts
Concentrated Technology
 
PPT
XPages -Beyond the Basics
Ulrich Krause
 
PDF
UKLUG 2012 - XPages, Beyond the basics
Ulrich Krause
 
PPTX
Pantheon basics
Plasterdog Web Design
 
KEY
Features, Exportables & You
jskulski
 
PPT
Crossover
szlwzl
 
PDF
Webinar: From Frustration to Fascination: Dissecting Replication
Howard Greenberg
 
PDF
May 2014-webinar
Howard Greenberg
 
PPTX
NMS Media Services Jobshet 1 to 5 Summary
Hock Leng PUAH
 
PPTX
EF Core (RC2)
Ido Flatow
 
PPTX
CollabSphere 2018 - Java in Domino After XPages
Jesse Gallagher
 
PPTX
Test studiowebinaraugcodedstep
Dhananjay Kumar
 
KEY
Builder webinar
Adam Christian
 
PowerShell crashcourse for sharepoint
Concentrated Technology
 
Securely connecting to apps over the internet using rds
Concentrated Technology
 
PowerShell custom properties
Concentrated Technology
 
Virtualization auditing & security deck v1.0
Concentrated Technology
 
Rapidly deploying software
Concentrated Technology
 
Prepping software for w7 deployment
Concentrated Technology
 
Wsus sample scripts
Concentrated Technology
 
XPages -Beyond the Basics
Ulrich Krause
 
UKLUG 2012 - XPages, Beyond the basics
Ulrich Krause
 
Pantheon basics
Plasterdog Web Design
 
Features, Exportables & You
jskulski
 
Crossover
szlwzl
 
Webinar: From Frustration to Fascination: Dissecting Replication
Howard Greenberg
 
May 2014-webinar
Howard Greenberg
 
NMS Media Services Jobshet 1 to 5 Summary
Hock Leng PUAH
 
EF Core (RC2)
Ido Flatow
 
CollabSphere 2018 - Java in Domino After XPages
Jesse Gallagher
 
Test studiowebinaraugcodedstep
Dhananjay Kumar
 
Builder webinar
Adam Christian
 

Viewers also liked (19)

PPTX
Best free tools for win database admin
Concentrated Technology
 
PPTX
Implementing dr w. hyper v clustering
Concentrated Technology
 
PPT
Automating Active Directory mgmt in PowerShell
Concentrated Technology
 
PPTX
Combining output from multiple sources
Concentrated Technology
 
PPT
PowerShell v4 Desired State Configuration
Jason Stangroome
 
PPT
Automating ad with powershell
Concentrated Technology
 
PPTX
PowerShell and WMI
Concentrated Technology
 
PDF
Advanced Tools & Scripting with PowerShell 3.0 Jump Start - Certificate
Don Reese
 
PPT
Managing SQLserver
Concentrated Technology
 
PPT
No-script PowerShell v2
Concentrated Technology
 
PPT
Free tools for win server administration
Concentrated Technology
 
KEY
PowerShell and the Future of Windows Automation
Concentrated Technology
 
PPTX
Managing enterprise with PowerShell remoting
Concentrated Technology
 
PPTX
Basic PowerShell Toolmaking - Spiceworld 2016 session
Rob Dunn
 
PPT
Ad disasters & how to prevent them
Concentrated Technology
 
PPTX
Introduction to powershell
Salaudeen Rajack
 
PPT
PowerShell Functions
mikepfeiffer
 
PPTX
Best free tools for w d a
Concentrated Technology
 
PPTX
VDI-in-a-Box: Microsoft Desktop Virtualization for Smaller Businesses and Uses
Concentrated Technology
 
Best free tools for win database admin
Concentrated Technology
 
Implementing dr w. hyper v clustering
Concentrated Technology
 
Automating Active Directory mgmt in PowerShell
Concentrated Technology
 
Combining output from multiple sources
Concentrated Technology
 
PowerShell v4 Desired State Configuration
Jason Stangroome
 
Automating ad with powershell
Concentrated Technology
 
PowerShell and WMI
Concentrated Technology
 
Advanced Tools & Scripting with PowerShell 3.0 Jump Start - Certificate
Don Reese
 
Managing SQLserver
Concentrated Technology
 
No-script PowerShell v2
Concentrated Technology
 
Free tools for win server administration
Concentrated Technology
 
PowerShell and the Future of Windows Automation
Concentrated Technology
 
Managing enterprise with PowerShell remoting
Concentrated Technology
 
Basic PowerShell Toolmaking - Spiceworld 2016 session
Rob Dunn
 
Ad disasters & how to prevent them
Concentrated Technology
 
Introduction to powershell
Salaudeen Rajack
 
PowerShell Functions
mikepfeiffer
 
Best free tools for w d a
Concentrated Technology
 
VDI-in-a-Box: Microsoft Desktop Virtualization for Smaller Businesses and Uses
Concentrated Technology
 
Ad

Similar to PS error handling and debugging (20)

PPTX
Error handling
Nitesh Bhat
 
PPTX
10 PowerShell Mistakes, Trips and Traps
Jeffery Hicks
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
PDF
Powershell training material
Dr. Awase Khirni Syed
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
PPTX
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
PPTX
Everything you need to know about PowerShell
Shane Hoey
 
PPTX
Invoke-Obfuscation nullcon 2017
Daniel Bohannon
 
PPTX
Invoke-Obfuscation DerbyCon 2016
Daniel Bohannon
 
PDF
PowerShell In 2024 And Beyond The Future Of Automation.pdf
DataSpace Academy
 
PPTX
Power shell training
David Brabant
 
PPTX
PowerShell for the Anxious ITPro
Jason Himmelstein
 
PDF
powershell.pdf
Saurabh846965
 
PPTX
PowerShell crash course
Concentrated Technology
 
PPT
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Concentrated Technology
 
PPTX
Powershell Tech Ed2009
rsnarayanan
 
PDF
Powershell notes
Carlos Amorim
 
PPTX
[CB16] Invoke-Obfuscation: PowerShell obFUsk8tion Techniques & How To (Try To...
CODE BLUE
 
PDF
Modernizing PowerShell Scripts
Axxes IT Consultancy
 
Error handling
Nitesh Bhat
 
10 PowerShell Mistakes, Trips and Traps
Jeffery Hicks
 
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Powershell training material
Dr. Awase Khirni Syed
 
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
Everything you need to know about PowerShell
Shane Hoey
 
Invoke-Obfuscation nullcon 2017
Daniel Bohannon
 
Invoke-Obfuscation DerbyCon 2016
Daniel Bohannon
 
PowerShell In 2024 And Beyond The Future Of Automation.pdf
DataSpace Academy
 
Power shell training
David Brabant
 
PowerShell for the Anxious ITPro
Jason Himmelstein
 
powershell.pdf
Saurabh846965
 
PowerShell crash course
Concentrated Technology
 
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Concentrated Technology
 
Powershell Tech Ed2009
rsnarayanan
 
Powershell notes
Carlos Amorim
 
[CB16] Invoke-Obfuscation: PowerShell obFUsk8tion Techniques & How To (Try To...
CODE BLUE
 
Modernizing PowerShell Scripts
Axxes IT Consultancy
 
Ad

More from Concentrated Technology (8)

PPTX
Wsus best practices
Concentrated Technology
 
PPT
Virtualization today
Concentrated Technology
 
PPT
Top ESXi command line v2.0
Concentrated Technology
 
PPTX
Managing SQLserver for the reluctant DBA
Concentrated Technology
 
PPTX
Inventory your network and clients with PowerShell
Concentrated Technology
 
PPT
Iis implementation
Concentrated Technology
 
PPT
Hyper v r2 deep dive
Concentrated Technology
 
PPT
How to configure esx to pass an audit
Concentrated Technology
 
Wsus best practices
Concentrated Technology
 
Virtualization today
Concentrated Technology
 
Top ESXi command line v2.0
Concentrated Technology
 
Managing SQLserver for the reluctant DBA
Concentrated Technology
 
Inventory your network and clients with PowerShell
Concentrated Technology
 
Iis implementation
Concentrated Technology
 
Hyper v r2 deep dive
Concentrated Technology
 
How to configure esx to pass an audit
Concentrated Technology
 

Recently uploaded (20)

PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
The Future of Artificial Intelligence (AI)
Mukul
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Simple and concise overview about Quantum computing..pptx
mughal641
 

PS error handling and debugging

  • 1. PowerShell Error Handling and Debugging Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC
  • 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. CAUTION: 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) • Don Jones • ConcentratedTech.com
  • 4. Agenda Debugging: Why and How Trace Code Breakpoints Error Handling How-to How-not-to • Don Jones • ConcentratedTech.com
  • 5. Debugging 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. • Don Jones • ConcentratedTech.com
  • 6. Logic 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. • Don Jones • ConcentratedTech.com
  • 7. Technique #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 • Don Jones • ConcentratedTech.com
  • 8. Technique #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 • Don Jones • ConcentratedTech.com
  • 9. Technique #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 • Don Jones • ConcentratedTech.com
  • 10. Remember! No expectation = No debugging You can only debug when you detect a difference between expectations and reality Let ’s walk through an example • Don Jones • ConcentratedTech.com
  • 11. Error 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 • Don Jones • ConcentratedTech.com
  • 12. DO NOT Do not set $ErrorActionPreference globally within a script Doing so suppresses errors – even the ones telling you about a typo or something • Don Jones • ConcentratedTech.com
  • 13. Option #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 • Don Jones • ConcentratedTech.com
  • 14. Final Exam Let ’s build a script, debug it, and include error handling – all from scratch • Don Jones • ConcentratedTech.com
  • 15. 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! • Don Jones • ConcentratedTech.com
  • 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

Editor's Notes

  • #2: MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.