#############################################################################Seems simple enough. So I set up a script to test my idea, and I cannot get it to work at all. Here is how I am testing this:
##
## 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
}
}
$Value = $True
Set-PSBreakpoint -Variable Value -Mode Write
$Value = $False
$Value = $False
$Value = $true
$value = $true
Get-PSBreakpoint | Remove-PSBreakpoint
So in using -Mode Write, this stops execution immediately before a new value is written to the variable, so it stops every time. This does not work for me because I need to know what $value is being set as, but it breaks before it is set. Is there a magical way around this?
Not sure this is what you're looking for; there is no direct way, but you can parse the command and act on the value that $Value is assigned.
ReplyDelete# tested interactvely only, mind word wrap
$Value = $True
Set-PSBreakpoint -Variable Value -Mode write -Action {
$v1 = $MyInvocation.MyCommand -replace '^.*Value\s*=\s*(\S+).*$', '$1'
[Bool]$v2 = Invoke-Expression $v1
if (-not $v2) {Write-Host -f 14 $v2}
}
$Value = $False
$Value = $False
$Value = $true
$value = $true
Get-PSBreakpoint | Remove-PSBreakpoint