# encoding: ascii
# api: powershell
# title: Enable-BreakOnError.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: script
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2138
# x-archived: 2016-05-17T12:52:24
# x-published: 2011-09-09T21:40:00
#
#
#############################################################################
##
## Enable-BreakOnError
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Creates a breakpoint that only fires when PowerShell encounters an error
.EXAMPLE
PS >Enable-BreakOnError
ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
0 Out-Default ...
PS >1/0
Entering debug mode. Use h or ? for help.
Hit Command breakpoint on 'Out-Default'
PS >$error
Attempted to divide by zero.
#>
Set-StrictMode -Version Latest
## Store the current number of errors seen in the session so far
$GLOBAL:EnableBreakOnErrorLastErrorCount = $error.Count
Set-PSBreakpoint -Command Out-Default -Action {
## If we're generating output, and the error count has increased,
## break into the debugger.
if($error.Count -ne $EnableBreakOnErrorLastErrorCount)
{
$GLOBAL:EnableBreakOnErrorLastErrorCount = $error.Count
break
}
}