1

Another question addresses how to debug a Powershell cmdlet written in C# in Visual Studio 2015-19: Debugging / unloading PowerShell Cmdlet

I could somehow invoke a script like this:

function Start-DebugPowerShell
{
    PowerShell -NoProfile -NoExit -Command {
        function prompt {
            $newPrompt = "$pwd.Path [DEBUG]"
            Write-Host -NoNewline -ForegroundColor Yellow $newPrompt
            return '> '
        }
    }
}
Set-Alias -Name sdp -Value Start-DebugPowerShell

This makes it easy to create a temp PS instance that you can exit to unload.

Or I can set the Debug options in the VS Project like this and it works great: enter image description here

I'd like to do the same thing, but using Visual Studio Code. I know how to debug this using attach (e.g. with these launch.json settings:

    {
        "name": ".NET Core Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "${command:pickProcess}",
        "justMyCode": false
    }

I can't figure out how to craft a launch.json that first launches pwsh. The project type is csharp and thus the only types available in launch.json are coreclr and clr.

1 Answer 1

0

Late to answer, but what you are looking for is the configuration that should be used to debug a PS module written in C#. This can be done via following configuration added to lanuch.json:

 {
     "name": "PowerShell cmdlets: pwsh",
     "type": "coreclr",
     "request": "launch",
     "preLaunchTask": "build",
     "program": "pwsh",
     "args": [
         "-NoExit",
         "-NoProfile",
         "-Command",
         "Import-Module ${workspaceFolder}/myModule/bin/Debug/netstandard2.0/myModule.dll",
     ],
     "cwd": "${workspaceFolder}",
     "stopAtEntry": false,
     "console": "integratedTerminal"
 }

Notice the line Import-Module as this has to point to the dll which is the PS Module. Then when you run this configuration in VSC and setup some breakpoints in C#, you can invoke your cmdlet in the builtin Terminal and it will start the debugging session.

MS has a very good documentation on that: link

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.