我开始使用Visual Studio Code编写Powershell脚本。我想要关闭未签名代码的检查,但是找不到如何操作。在论坛中也没有找到相关信息。
我开始使用Visual Studio Code编写Powershell脚本。我想要关闭未签名代码的检查,但是找不到如何操作。在论坛中也没有找到相关信息。
你可以通过向powershell.exe命令添加参数来调整策略。 为此,请打开设置JSON文件。然后添加以下行:
"terminal.integrated.shellArgs.windows": "-ExecutionPolicy ByPass",
编辑
如此答案所述,上述答案可能已过时,而且VS Code弃用。请参见此答案中的替代方法,或者按照在VS Code帖子中看到的示例,但添加"args"
来设置
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"overrideName": true,
"icon": "terminal-powershell",
// "env": {
// "TEST_VAR": "value"
// },
// -------
// this part is needed:
"args": [ "-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
我通过设置以下策略来解决:
Set-ExecutionPolicy –ExecutionPolicy RemoteSigned
以管理员身份运行 Visual Code 集成开发环境。
以下博客清楚地列出了解决步骤:
PowerShell的默认设置有助于防止运行恶意脚本。默认情况下,您可以在VSCode集成终端上运行脚本。
要更改PowerShell安全设置,请以管理员身份打开PowerShell并运行以下命令:
Get-ExecutionPolicy -List
你应该会得到像这样的回复:
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser RemoteSigned
LocalMachine AllSigned
由于您无法在VSCode终端上运行脚本,因此CurrentUser很可能未定义。
要更改此情况,请运行以下命令:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
重新打开VSCode终端,现在应该可以正常工作。我在Visual Studio Code中使用了以下命令,每次都有效。您可以尝试使用此命令来解决PowerShell权限问题。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
建议的命令对我没有起作用,但是下面的方法已经解决了问题,https://caiomsouza.medium.com/fix-for-powershell-script-not-digitally-signed-69f0ed518715。但是它只适用于单个会话。
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
-Scope
不是 Process
,可能会在多个会话中运行。也许是 User
? - zzxyz
help about_Execution_Policies
。 - Bill_Stewart