Documents

32 pages
212 views

The Big Book of PowerShell Error Handling

of 32
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
The Big Book of PowerShell Error Handling
Transcript
  The Big Book of PowerShell Error Handling Don Jones This book is for sale at http://leanpub.com/thebigbookofpowershellerrorhandlingThis version was published on 2017-01-06This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishingprocess. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools andmany iterations to get reader feedback, pivot until you have the right book and build traction onceyou do.© 2016 - 2017 Don Jones  Contents The Big Book of PowerShell Error Handling  . . . . . . . . . . . . . . . . . . . . . . . . . .  1Introduction  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3 What is error handling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3How this book is organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Powershell Error Handling Basics  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  4 ErrorRecords and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Terminating versus Non-Terminating Errors . . . . . . . . . . . . . . . . . . . . . . . . . 5 Controlling Error Reporting Behavior and Intercepting Errors  . . . . . . . . . . . . . . .  7 The $Error Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7ErrorVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8$MaximumErrorCount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8ErrorAction and $ErrorActionPreference . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Try/Catch/Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Trap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12The $LASTEXITCODE Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13The $? Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Analysis of Error Handling Test Results  . . . . . . . . . . . . . . . . . . . . . . . . . . . .  16 Intercepting Non-Terminating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Intercepting Terminating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Effects of setting ErrorAction or $ErrorActionPreference . . . . . . . . . . . . . . . . . . 18How PowerShell behaves when it encounters unhandled terminating errors . . . . . . . . 19Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Putting It All Together  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  21 Suppressing errors (Mostly, don’t do this) . . . . . . . . . . . . . . . . . . . . . . . . . . 21The $? variable (Use it at your own risk) . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Determining what types of errors can be produced by a command . . . . . . . . . . . . . 22Dealing with Terminating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Dealing with Non-Terminating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Calling external programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26  CONTENTS Afterword  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  28
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x