3

Looks like this may have been asked last year but wasn't answered.

I have been writing applications written in PowerShell using VSCode for development. I am starting to write some standard class libraries in C# and found out that I can also do C# development in VSCode using omnisharp.

What I am hoping that I will be able to do is the following:

  • Start debugging in my PowerShell script as I normally would

  • Move the debugger into my C# class so I can troubleshoot issues in my class library

Is this possible? I'm having trouble figuring out where to start. I'm guessing I'll need to do something to build the library in such a way it's able to be debugged and tie it to the PowerShell sessions somehow.

Any help or advice on how to get started would be greatly appreciated.

Efie
  • 1,430
  • 2
  • 14
  • 34
  • 2
    "[...] asked last year but wasn't answered" - got a link? :) – Mathias R. Jessen Oct 18 '20 at 16:22
  • Here's the one I was talking about: https://stackoverflow.com/questions/59445038/can-i-debug-a-dependent-assembly-from-a-powershell-module – Efie Oct 18 '20 at 16:47
  • What i have done normally is, write a method outside the powershell cmdlet and test that out within visual studio.. once it's tested successfully, tie it in to the cmdlet. Dont write any code within the cmdlet cs file, but call the method with required variables. – Jawad Oct 18 '20 at 17:36
  • I'm afraid you can't transition between debugging sessions in two different debuggers. You'll want to test and debug the classlib and script/module independently – Mathias R. Jessen Oct 18 '20 at 19:20
  • 2
    I am assuming that what you need is a DebugBreak() (aka int3) called from your c# class library. For c#, it is called Debugger.Break().. but there is also Debugger.Launch().. see [this](https://stackoverflow.com/questions/4733763/debugbreak-equivalent-in-c-sharp) link. This is how you debug extensions you have written for other applications. In this case, your "extension" is c# and your application is powershell. – Señor CMasMas Oct 18 '20 at 19:44
  • I have the same question, myself. I tried adapting [my answer](https://stackoverflow.com/a/35300427/150605) to [How do I debug a powershell module in Visual Studio](https://stackoverflow.com/q/35298963/150605) by creating a configuration in `launch.json` with the `"program"` property set to `"pwsh.exe"`. The problem I ran into is that, evidently, VSCode insists on surrounding the elements of `"args"` with double quotes, and what should be passed as `-Command ...` ends up being `"-Command ..."`. Perhaps using the default/`-File` parameter to pass a script path is the way to go. – Lance U. Matthews Dec 15 '20 at 23:09
  • [Using Visual Studio Code to debug compiled cmdlets](https://learn.microsoft.com/powershell/scripting/dev-cross-plat/vscode/using-vscode-for-debugging-compiled-cmdlets) describes exactly what I was trying to do. I bet you could use `-File` to launch your script, but you wouldn't be able to debug it. You'd have to have one debug configuration for C# and one for PowerShell so you can debug one or the other. – Lance U. Matthews Dec 15 '20 at 23:38

0 Answers0