4

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. 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. Is there a way to attach ISE (or a similar debugger) to a PowerShell script that is being hosted within the C# program?

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. 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.

3
  • Just to clarify, debugging in powershell isn't dependent on the ISE. The debugging commands are built into the language, ISE just provides a GUI drop down menu that essentially just executes those commands. So the answer to the question is: the debugger is already "attached" Commented May 21, 2014 at 18:53
  • 1
    I was not previously aware of this fact. Looking around I do see that PowerShell provides its own debugging facilities, and this is likely a good starting point. Commented May 21, 2014 at 19:45
  • True, but you cannot use these features in your C# application without implementing a debugger that utilizes them. Commented May 21, 2014 at 23:19

2 Answers 2

2

There is currently no way of attaching a debugger capable of debugging PowerShell to an arbitrary host process.

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 (e.g. wait for the user to say "go" or "step".)

Sign up to request clarification or add additional context in comments.

2 Comments

This changes in PS 5.0
Yes, the Enter-PSHostProcess cmdlet along with Get-Runspace and Debug-Runspace, new to V5, help here.
1

@Jason Shirk is correct about the debugger. 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. In spite of the name the debugger works with any host (in particular, the NuGet console does not have its own debugger implemented). Note that its UI is very minimalistic, somewhat similar to MS PS console debugger. 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. In the ideal world ISE could provide it, I think. But it currently does not.

1 Comment

Hi Roman, you really should indicate in your answer that you're the author of the package. meta.stackexchange.com/a/156186/193205

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.