Search code examples
c#powershellvisual-studio-debugging

Debugging - Step into PowerShell Script Executed via API


I'm using System.Management.Automation.PowerShell to programmatically execute a PowerShell script from C# application. The PowerShell script loads a .Net dll which it uses to perform its activities.

var script = "Add-Type -Path 'MyLibrary.dll'; ....";

using (var powershell = PowerShell.Create()) {
    powershell.AddScript(script);

    powershell.Invoke();
}

Is it possible to somehow connect Visual Studio's debugger to the PowerShell object instance so that the debugger can seamlessly step from the C# application into the PowerShell script and from that script into MyLibrary.dll (assuming I have symbols for the dll)?

Edit: Based on the below, it appears that there may not be a way to seamlessly flow debugging in Visual Studio from C# to PowerShell. However, it is possible to use VS to debug the C# code that launches and that is launched by PowerShell.


Solution

  • You could debug the dll file by calling the following in your helper class:

    System.Diagnostics.Debugger.Launch();