我上周就在Powershell和Devops Summit。很高兴见到我在网上工作的许多人。我也走了几个想法,我想去工作。

一个想法坐在我身边 Kirk Munro在调试时的会话。我正在编写很多笔记并反思我如何调试我的脚本。然后我意识到我可以制作一个简单的功能来使其更容易。

指数

这个想法

I often check the $error[0].ScriptStackTrace for the source of an error and then go place a breakpoint where the error was raised. I realized that I could parse the ScriptStackTrace and call Set-PSBreakPoint directly. It is a fairly simple idea and it turned out to be just as easy to write.

Psgraph..

把它整合在一起

If you ever looked at a ScriptStackTrace on an error, you would see something like this:

PS> $error[0].ScriptStackTrace
at New-Error, C:\workspace\ConvertTo-Breakpoint\testing.ps1: line 2
at Get-Error, C:\workspace\ConvertTo-Breakpoint\testing.ps1: line 6
at <ScriptBlock>, C:\workspace\ConvertTo-Breakpoint\testing.ps1: line 9

虽然数据只是一个字符串,但它非常一致且易于与正则表达式解析。这是我曾经匹配每行的正则表达式模式: at .+, (?<Script>.+): line (?<Line>\d+)

我有点花哨和使用 命名为子表达式匹配. I do this so I can access them by name with $matches.Script and $matches.Line.

Once I had the data that I needed, it was a quick call to Set-PSBreakPoint to set the breakpoint.

Set-PSBreakPoint -Script $matches.Script -Line $matches.Line

I put a bit more polish on it and called it ConvertTo-Breakpoint.

我在这次视频中逐步走路一步一步走路: 转换 - 断点:编写cmdlet.

如何使用

This is the cool part. I can now take any $error and pipe it to ConvertTo-Breakpoint. Then a breakpoint will be created where the error was thrown.

$error[0] | ConvertTo-BreakPoint

我添加了正确的管道支持,以便您可以给它所有的错误。

$error | ConvertTo-BreakPoint

I even added -All as a switch to create a breakpoint at each point in the callstack instead of just the source of the error.

$error[0] | ConvertTo-BreakPoint -All

vscode调试器

In my experimentation with VSCode and Set-PSBreakpoint; I discovered that you have to have the debugger running for Set-PSBreakpoint to set breakpoints. There is an issue on github about this already. This is why I did the demo video in the ISE.

我在哪里找到它?

这已经在Psgallery发表。您可以安装它并立即开始尝试。

Install-Module -Name ConvertTo-Breakpoint -Scope CurrentUser

如果您想结帐来源,我将其在Github上发布了所有其他工具:

更多视频

我尝试了新的东西并从一开始就恢复了创建这个模块。我走过这个想法,原型它,创建测试,创建函数,构建模块,添加构建脚本,并发布它。整个过程被捕获了。

让我知道 推特 如果这是你希望看到我做更多的事情。我很想听到你的反馈。