PoshCode Archive  Artifact [5bbaba2f6a]

Artifact 5bbaba2f6a310e0c9a9ebbbd98c03233504dafa2c3dfa02ccbf140c167a2007f:

  • File Enable-BreakOnError.ps1 — part of check-in [42c8cdfc31] at 2018-06-10 13:05:41 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1562)

# 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
    }
}