Archive for January, 2014

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.

Leave a comment