Enable debugging on terminating errors in PowerShell

PowerShell provides several alternative actions on errors:

  • SilentlyContinue
  • Stop
  • Continue
  • Inquire
  • Ignore (V3)

They are set by the variable $ErrorActionPreference in the current scope as the default for all commands or by the common parameter ErrorAction for an individual command.

Unfortunately there is no such an action as Debug on terminating errors which is useful on troubleshooting. The closest that we can get out of the box is the error action Inquire. It is useful in many cases but it is not always convenient. For example, it may produce a lot of noise on legitimate non terminating errors. Also, although it allows entering the nested prompt (Suspend) for examining the current situation this is not the same as entering the debugging mode which allows stepping further, for example.

Fortunately there is a way to enable debugging on terminating errors. It exploits the fact that the system variable StackTrace is updated in such cases. Thus, setting the variable breakpoint for StackTrace writes actually turns debugging on terminating errors on.

Here is the simple script Debug-Error.ps1 which turns debugging on terminating errors on and off. Ideally, it should be located in the path, so that commands Debug-Error and Debug-Error -Off are always available.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: