简体   繁体   中英

Visual Studio Code PowerShell code debugging from the terminal

I have PowerShell script opened in the code window. I am starting the same script from the terminal and expect it to run in the debugging mode. It means: script would stop at breakpoint.

Described behavior would be similar to PowerShell ISE. However, it runs whole script without stopping at breakpoints.

is there a way to achieve script debugging when started from the terminal?

When you execute a PowerShell console, you can use this command below to set a breakpoint at a given line of code within your script, then specify the script. When your script runs, it will break at the given point you requested.

Set-PSBreakpoint -Line 9  -Script C:\temp\test_script.ps1

Unfortunately, as of v2019.5.0 of the PowerShell extension for Visual Studio Code , there is no support for debugging via an external console window.

A longstanding feature request to support this scenario is in this GitHub issue , but it has yet to be implemented - I suggest you up-vote the proposal there.
Update : I seems that improvements are now actively being worked on .

Workarounds , as of v2019.5.0:

On Windows , using an external PowerShell Core console ONLY :

  • Open the folder in which your script is located for editing, not just the script file itself.

  • Switch to the Debug view (View > Debug)

  • If the dropdown list in the top-left corner shows No Configurations , click on the cog wheel icon to its right:

    • If prompted for an environment, choose PowerShell .
    • A .vscode/launch.json file is created in your folder with debugging launch configurations that underlie the choices offered in the dropdown list in the Debug view.
  • Select PowerShell Attach to Host Process in the dropdown list.

Apart from needing to open the entire folder in future sessions, the above steps should only be necessary once .

To start a debugging session:

Note: Debugging requires the PowerShell Integrated Console to be running, which is currently not automatically ensured. If you activate a PowerShell script in the editor at least once, the PowerShell Integrated Console will be launched, as reflected in the dropdown list showing the active shell in top-right corner of the integrated terminal.

  • Press F5 (Debug > Start Debugging).

  • You'll be prompted for a target process to attach to: Pick the PowerShell Core session to attach to; all such sessions are prefixed with pwsh and show their console-window title as well as the PID (process ID); when in doubt, execute $PID in the target console window to determine its PID.

  • Pick runspace 1 as the target runspace (even though it says Busy ).

    • Note: You could skip this step in the future by hard-coding the use of runspace 1 into your launch configuration in .vscode/launch.json : Add the following line to the JSON object that defines configuration "PowerShell Attach to Host Process" : "runspaceId": 1

The debugger should now be attached to your external PowerShell Core console, as reflected in the status output in the integrated terminal.

Note that Visual Studio Code does not automatically activate when the debugger hits a breakpoint, and it also doesn't deactivate automatically when execution is resumed - you'll have to manually switch between your external console and Visual Studio Code.

  • Switch to the external PowerShell Core console and invoke the script that you want to debug.

    • The first time in a session, the debugger will break right away , namely at the end of the PSConsoleHostReadLine function
    • either start stepping into your script from there, or simply press F5 (Debug > Continue) to continue execution until a previously set breakpoint is hit.
  • To stop debugging, use Shift + F5 (Debug > Stop Debugging) or press the disconnect icon (depicting a red plug) on the debugging toolbar.


With Windows PowerShell / on other platforms the closest you can come with the current capabilities is to use the integrated terminal instead, via debug-launch configuration PowerShell Interactive Session .

Note: PowerShell Core on macOS and Linux should support the debugging technique described above for Windows, but that didn't work for me on my macOS 10.14.6 machine with PowerShell Core 7.0.0-preview.2; YMMV.

The limitations are:

  • No PSReadLine functionality will be available - notably no command recall with the up-arrow key.

  • The integrated terminal shares the Visual Studio Code window with the editor windows, so you can't pop out the terminal to a separate window that you could place on a separate monitor, for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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