Visual Studio Code 未签名的 Powershell 脚本

18

我开始使用Visual Studio Code编写Powershell脚本。我想要关闭未签名代码的检查,但是找不到如何操作。在论坛中也没有找到相关信息。


4
从PowerShell提示符中运行命令:help about_Execution_Policies - Bill_Stewart
7个回答

20

你可以通过向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",

1
如果您不需要在 vscode 之外运行脚本,这是最安全的选项。 - Eugene
1
由于问题涉及使用Visual Studio Code进行Powershell脚本编写,我认为这是最佳答案。 - rpet
提示:保存更改后,需要重新启动Visual Studio Code。 - Blarzek
7
在最新的 VS Code 版本中,似乎此设置已被弃用。 - Eric
这是截至2023年6月的最佳答案,因为直接在行内设置执行策略只对该集成终端/VSCode会话的其余部分有效。 - HaulinOats
直到我将下载的ps1文件的内容复制到一个新创建的文件中,签名约束才消失。 - undefined

16

14

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终端,现在应该可以正常工作。
如果您想了解更多详细信息,请访问此处的完整文档:https://learn.microsoft.com/es-es/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7

2

前往选择默认 shell

输入图像描述

然后选择命令提示符而不是Windows PowerShell,它会起作用。

输入图像描述


2
您可以使用命令Set-ExecutionPolicy更改系统的执行策略。您需要从管理员会话中执行此操作。您可以通过在PowerShell提示符下使用命令help Set-ExecutionPolicy找到适当的语法。
您还可以在线查找命令参考,例如SS64Technet
在Stack Overflow上也有一个非常明显的问答,其中包含相同的信息。

我尝试了。这是我得到的结果:PS C:\Users\admrschoenhard\Desktop> Set-ExecutionPolicy undefined PS C:\Users\admrschoenhard\Desktop> c:\Users\admrschoenhard\Desktop\Untitled-1.ps1 - Ryan Schoenhard
导入模块:无法加载文件\us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1。文件\us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1没有数字签名。您不能在当前系统上运行此脚本。有关运行脚本和设置执行策略的更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=135170中的about_Execution_Policies。 位于C:\Users\admrschoenhard\Desktop\Untitled-1.ps1的第1个字符:1
  • import-module \us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1
- Ryan Schoenhard
  • CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException
    • FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
- Ryan Schoenhard
快速备注 - 当我注释掉导入模块语句时,它允许我将执行策略设置为未定义。只有在尝试导入未签名的模块时,我才会收到错误消息,表明我不能将策略设置为未签名。 - Ryan Schoenhard
看起来我误解了你想做的事情。禁用未签名脚本是可以在管理员交互式 shell 中完成的。如果你可以将其放入任何脚本中,那么这个命令的目的就会被破坏。至于 Visual Studio Code,在尝试使用 PowerShell IDE 时会发生什么?编辑你的问题,包括现有的代码和输出错误。在评论区里讨论很麻烦。Blueshell 看起来是一个专业的软件包,但我找不到 psm1 文件的页面。他们的指南对安装它有什么说明? - YetAnotherRandomUser

0

我在Visual Studio Code中使用了以下命令,每次都有效。您可以尝试使用此命令来解决PowerShell权限问题。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

0

如果 -Scope 不是 Process,可能会在多个会话中运行。也许是 User - zzxyz

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