powershell - VSCode 覆盖 PowerShell ExecutionPolicy

标签 powershell visual-studio-code registry vscode-settings

使用 Get-ExecutionPolicy -list 时在 VSCode for PowerShell v5 x64 & x86 中,它返回以下内容:

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process          Bypass
  CurrentUser       Undefined
 LocalMachine       Undefined

在 VSCode 中使用相同的命令并使用 PowerShell v7 控制台时,它会返回:
        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process    RemoteSigned
  CurrentUser       Undefined
 LocalMachine    RemoteSigned

有 2 个注册表项设置了 ExecutionPolicy(原文:ByPass)
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command      REG_SZ  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process RemoteSigned }; & '%1'" 
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\0\Command        REG_SZ  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process RemoteSigned }; & '%1'"

这 2 个条目最初设置为将执行策略设置为 ByPass,我已手动将这 2 个条目设置为 RemoteSigned。 (为什么这两个条目在注册表中,为什么它们被设置为绕过?!)。

注册表中没有为 PowerShell 设置更多策略,然后是这 2 个条目。
将这些设置为 RemoteSigned 似乎已经完成了 v7 的技巧,但不适用于 v5。

在 v5 PowerShell 控制台中检查 ExecutionPolicy 时,它们都是未定义的。

检查 vscode-powershell.log 时,它说它使用参数启动 PowerShell:PowerShell args: -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command Import-Module ...
我可以看到 vscode powershell 扩展在以下文件中设置了其中一些参数:
C:\Users\UserName.vscode\extensions\ms-vscode.powershell-2020.3.0\out\src\process.js
但我不确定这是否是进行这些更改的正确文件。

我希望能够在启动语言服务器时检查/将 VSCode 的 ExecutionPolicy 设置为 RemoteSigned,我该在哪里执行此操作?

另外,当 ExecutionPolicy 不等于 AllSigned 时,为什么注册表中有 2 个条目将 ExecutionPolicy 设置为 ByPass。这可能是在使用他们的单行安装程序安装 Chocolatey 后发生的:Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
我没有使用任何配置文件。

我知道它有点乱,很抱歉,我的主要问题是 VSCode 在启动语言服务器时设置 ExecutionPolicy 的位置,我可以更改它吗?
在进行一些调查时,我发现有 2 个注册表项将 ExecutionPolicy 设置为 bypass ,这是为什么,是标准,看起来像一个安全问题。 (这可能是由于安装巧克力造成的,不确定)

最佳答案

我不知道这对解决此问题的其他用户是否有任何帮助,但是从 VSCode 文档中阅读推荐的方法是在 settings.json 内添加 Powershell 的新配置文件,无需全局更改当前策略或使用扩展。这些方面的东西:

"terminal.integrated.profiles.windows": {
     "PowerShell": {
         "source": "PowerShell",
         "args": [
             "-ExecutionPolicy",
             "Bypass"
         ]
     }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",

关于powershell - VSCode 覆盖 PowerShell ExecutionPolicy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61182855/

相关文章:

Powershell 启动进程、超时等待、杀死并获取退出代码

arrays - 如何仅显示文本文件数组中的“读取主机”值?

powershell - PowerShell:为什么Out-File将长行分成较小的行?

c# - 如何以最简单的方式在 V 2010Express C# 中创建 MRU?

Delphi - 将 NtCreateKey 与 HKCU 结合使用

windows - 通过 Powershell 或 BATCH 序列化执行 symstore

visual-studio-code - 如何更快地显示 Visual Studio Code 自动完成建议?

javascript - 如何在vscode的外部文件中获取express中间件的智能感知?

python - vscode-python pipelinenv 自动运行行为是否存在安全风险?

registry - 如何从文档中的菜单上下文中删除 git?