简体   繁体   English

调试PowerShell脚本

[英]Debugging PowerShell script

I am interested in developing a C# application that processes data, and to aid in filtering the data I plan to expose part of the application as an interpreted script via PowerShell. 我有兴趣开发一个处理数据的C#应用​​程序,并帮助过滤我计划通过PowerShell将部分应用程序作为解释脚本公开的数据。 I think I have a good grasp on how to accomplish that ( How to run PowerShell scripts from C# ), but it would be extremely valuable to offer debugging capabilities like those offered in PowerShell ISE . 我认为我已经很好地掌握了如何实现这一点( 如何从C#运行PowerShell脚本 ),但提供PowerShell ISE中提供的调试功能将非常有价值。 Is there a way to attach ISE (or a similar debugger) to a PowerShell script that is being hosted within the C# program? 有没有办法将ISE(或类似的调试器)附加到C#程序中托管的PowerShell脚本?

Part of the reasoning behind this is the flexibility that some of the components are fixed in nature, hence the compiled C# executable, but some of it needs to be easily altered at runtime. 这背后的部分原因是一些组件本质上是固定的灵活性,因此编译的C#可执行文件,但其中一些需要在运行时轻松更改。 I envision a DataTable or List<> being passed from C# into PowerShell, with a similar return value from the script that would have parsed data. 我设想一个DataTable或List <>从C#传递到PowerShell,具有类似于将解析数据的脚本的返回值。

There is currently no way of attaching a debugger capable of debugging PowerShell to an arbitrary host process. 目前无法将能够调试PowerShell的调试器附加到任意主机进程。

It is possible to implement your own debugger - the necessary hooks are public, but this is a non-trivial task - you would need to implement the UI - you call an api to set breakpoints, then PowerShell will call you when a breakpoint is hit, at which point you decide what to do (eg wait for the user to say "go" or "step".) 可以实现自己的调试器 - 必要的钩子是公共的,但这是一个非常重要的任务 - 您需要实现UI - 您调用api来设置断点,然后PowerShell会在断点被命中时调用您,此时你决定做什么(例如,等待用户说“去”或“步”。)

@Jason Shirk is correct about the debugger. @Jason Shirk对调试器是正确的。 But instead of implementing your own you can use some existing. 但是,不是实现自己的,你可以使用一些现有的。 For example, a simple debugger implemented as a script included to the NuGet package NuGetDebugTools . 例如,一个简单的调试器实现为NuGet包NuGetDebugTools中包含的脚本。 In spite of the name the debugger works with any host (in particular, the NuGet console does not have its own debugger implemented). 尽管名称调试器适用于任何主机(特别是,NuGet控制台没有实现自己的调试器)。 Note that its UI is very minimalistic, somewhat similar to MS PS console debugger. 请注意,它的UI 非常简约,有点类似于MS PS控制台调试器。 But it is workable and it is better than no debugger at all. 但它是可行的,它比没有调试器好。

I hope the tip may be useful for debugging or as a starting point for implementing your own. 我希望这个提示可能对调试很有用,或者作为实现自己的提示的起点。 But it looks like you are actually looking for an external console-like window (not necessarily console based) related to the current PowerShell session/runspace, where you can explore exposed objects and change them. 但看起来您实际上正在寻找与当前PowerShell会话/运行空间相关的外部控制台式窗口(不一定是基于控制台),您可以在其中探索暴露的对象并进行更改。 In the ideal world ISE could provide it, I think. 我认为,在理想世界中,ISE可以提供它。 But it currently does not. 但它目前没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM