如何在VSCode中配置任务以调用PowerShell脚本

7
我将尝试在Visual Studio Code中设置构建任务,以调用已编写的PowerShell构建脚本。以下是我设置的构建任务代码:
{
    "version": "0.1.0",
    "command": "powershell",
    "isShellCommand": true,
    "args": [   
        "-File ${cwd}/source/deployment/build.ps1",
        "-NoProfile",
        "-ExecutionPolicy Unrestricted"
    ],
    "taskSelector": "-task ",
    "showOutput": "always",
    "tasks": [
        {
            "taskName": "build",
            "showOutput": "always",
            "isBuildCommand": true
        }
    ]
}

在执行任务时,出现以下输出:

无法加载文件 C:\Users\chkeita\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为系统禁用了脚本运行。有关更多信息,请参见 about_Execution_Policies:http://go.microsoft.com/fwlink/?LinkID=135170。 在行:1 字符:3 + . 'C:\Users\chkeita\Documents\WindowsPowerShell\Microsoft.PowerShell_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess -File:未将“-File”识别为可用的 cmdlet、函数、脚本文件或可操作程序的名称。请检查输入的拼写是否正确,如果包括路径,请验证路径是否正确并重试。 在行:1 字符:1 + -File f:\Dev\spf3/source/deployment/build.ps1 -NoProfile -executionpo ... + ~~~~~ + CategoryInfo : ObjectNotFound: (-File:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

我尝试重新排序参数并将它们合并成一个字符串,但没有成功。

我错过了什么?在 VSCode 中有更好的方法吗?


尝试更改:将“-ExecutionPolicy Unrestricted”更改为“Set-ExecutionPolicy -ExecutionPolicy Unrestricted”。 - seN
似乎路径需要更改为:-File ${cwd}\source\deployment\build.ps1;不确定“-File”是否是正确的命令,老实说。 - seN
这并没有帮助,这些是PowerShell命令的参数(https://technet.microsoft.com/en-us/library/hh847736.aspx),我在命令行中测试过它,它可以工作。 - Cheick
1个回答

12

这是一个可运行的版本,请查看讨论了解更多细节。

{
    "version": "0.1.0",
    "command": "powershell",
    "args": [   
        "-ExecutionPolicy",
        "Unrestricted",
        "-NoProfile",
        "-File",
        "${cwd}/source/deployment/build.ps1"       
    ],
    "taskSelector": "-task ",
    "showOutput": "always",
    "tasks": [
        {
            "taskName": "build",
            "showOutput": "always",
            "isBuildCommand": true
        }
    ]
}

明确一下:CLI参数,例如-ExecutionPolicy-NoProfile必须放在-File或(可能隐含的)-Command参数之前才能生效。否则它们将被视为要调用的脚本文件的参数/要执行的命令的一部分。 - mklement0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接