ConvertTo-Breakpoint

Among the huge number of useful modules on the internet, there is ConvertTo-Breakpoint by Kevin Marquette. The module is available on PowerShell Gallery and is being developed on GitHub at https://github.com/KevinMarquette/ConvertTo-Breakpoint. This module allows you to simply create breakpoints from error records, which has been a huge time-saver for us in the past.

Errors in PowerShell contains a lot of additional info, such as the target object, the category, and also the script stack trace. Kevin made use of that property to parse where in the script an issue occurred, to automatically set one or more new breakpoints. It is even possible to set breakpoints for all errors present in the stack trace. Take a look at the following code:

# Get the module
if (-not (Get-Module ConvertTo-Breakpoint -List))
{
Install-Module ConvertTo-Breakpoint -Scope CurrentUser -Force
}

# Execute the entire script and see your breakpoints appear
# Execute the script a second time to be placed in your breakpoint ;)
Write-Error 'Good lord... This went wrong'

$Error[0] | ConvertTo-Breakpoint

# In case of errors bubbling up, you can set breakpoints at
# all positions
function foo
{
[CmdletBinding()]
param ( )
throw 'Bad things happen'
}
function bar
{
[CmdletBinding()]
param ( )
try
{
foo -ErrorAction Stop
}
catch
{
Write-Error -Exception $_
}
}
function baz
{
bar -ErrorAction Continue
}
baz

# Now we get three break points.
# One at baz, where the exception bubbles up to
# One in baz, where bar is called
# One in bar, where the error of foo is rethrown
$error[0] | ConvertTo-Breakpoint -All

As shown in the previous code sample, by simply piping the error record to ConvertTo-Breakpoint, a breakpoint has been set. Just make sure that you save the entire sample script before running the script. Otherwise, no breakpoint can be placed. This usually happens when a compiled cmdlet throws an error. In this case, no stack trace will be available.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset