简体   繁体   中英

Cannot debug PowerShell scripts in Visual Studio Code

I am to run PowerShell scripts in Visual Studio Code with F5.

My Visual Studio Code version is 1.50.1 with commit d2e414d9e42

I get the following error message:

Cannot debug or run a PowerShell script until the PowerShell session has started. Wait for the PowerShell session to finish starting and try again.

I installed the official Microsoft PowerShell extension and my launch.json includes the following:

{
        "name": "PowerShell: Launch Script",
        "type": "PowerShell",
        "request": "launch",
        "script": "countcharacters.ps1",
        "cwd": "${workspaceFolder}"
}

How can I make debugging work with F5?

Closing all open PowerShell windows before pressing F5 fixed this issue for me.

You also may need to make sure that you don't have this Session exited warning:

会话退出警告

If you do, click "Restart Current Session", then try pressing F5 again. 重新启动当前会话选项

This is with the following config in launch.json (On a Windows 10 machine):

{
    "name": "PowerShell: Launch script.ps1",
    "type": "PowerShell",
    "request": "launch",
    "script": ".\\script.ps1",
    "cwd": "${workspaceFolder}"
}

To solve this problem, you can try to restart your current PowerShell session by:

  1. Opening the command pallet ( command + shift + p )
  2. Searching for Restart Current Session and selecting it
  3. Pressing the F5 button again

You may see the issue pop up once more, but just press F5 again and you should see things start to execute.

如果您的 PackageManagement 模块不是最新的,则可能会弹出此错误消息,VSCode 会接收到该错误消息,在屏幕右下方显示一个弹出窗口,询问您是否要更新并且在更新之前不考虑 Powershell 已启动被解雇。

安装最新版本的包管理,然后重新启动 PowerShell 会话或 VSCode 解决了该问题。

Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery

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