【问题标题】:Debug powershell in Visual studio code with parameters from command line使用命令行参数在 Visual Studio 代码中调试 powershell
【发布时间】:2017-01-24 16:19:13
【问题描述】:

使用 Powershell ISE 我经常在脚本中设置断点并从命令行启动脚本。然后 ISE 在断点处停止并让我从那里进行调试。如何从 Visual Studio Code 中的终端执行相同操作?下面是一个脚本,只是为了向您展示我的意思。从我会写的终端开始:

.\hello.ps1 -firstName "firstName" -lastName "theLastName"

但从终端执行此操作只会打开一个新窗口。

param(
[string]$firstName,
[string]$lastName
)

Write-Host "Starting test script"
try
{
   Write-Host "Hello $firstName $lastName"        
}
catch
{
    Write-Error "Error. Exception: $_"
}

write-host "Script done"

【问题讨论】:

    标签: debugging visual-studio-code


    【解决方案1】:

    要使Eickhel Mendoza 的链接中的信息明确 - 解决方案是设置一个运行和调试配置文件。这是在.vscode/launch.json 中创建的。本案例的内容为:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch Hello Script",
                "type": "PowerShell",
                "request": "launch",
                "script": "${workspaceFolder}\\hello.ps1",
                "cwd": "${workspaceFolder}",
                "args": ["-firstName \"firstName\" -lastName \"theLastName\""]
            }
        ]
    }
    

    然后只需打开 VSCode 上的“运行和调试”侧边栏窗格(带有错误的播放按钮或 Ctrl+Shift+D ),您可以在其中运行启动脚本,并且您设置的任何断点都将按预期命中。

    【讨论】:

      【解决方案2】:

      也许这可能会有所帮助...它涉及修改工作区中的 launch.json 文件。

      https://github.com/PowerShell/vscode-powershell/tree/master/examples#passing-arguments-to-the-script

      【讨论】:

      • 最好将建议作为评论而不是作为答案发布。
      猜你喜欢
      • 2020-07-16
      • 2010-09-22
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      相关资源
      最近更新 更多