VSCode 覆盖 PowerShell ExecutionPolicy

VSCode overrides PowerShell ExecutionPolicy

当在 VSCode 中为 PowerShell v5 x64 和 x86 使用 Get-ExecutionPolicy -list 时,returns 如下:

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

在 VSCode 中使用相同命令并使用 PowerShell v7 控制台时 returns:

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

有 2 个设置 ExecutionPolicy 的注册表项(最初:ByPass)

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell[=13=]\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[=13=]\Command        REG_SZ  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process RemoteSigned }; & '%1'"

这 2 个条目最初设置为 Set the Execution Policy to ByPass ,我已将这 2 个条目手动设置为 RemoteSigned。 (为什么这 2 个条目在注册表中,为什么将它们设置为绕过?!)。

除了这 2 个条目之外,注册表中没有为 PowerShell 设置更多策略。 将这些设置为 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
但我不确定这是否是进行这些更改的正确文件。

我希望能够在启动语言服务器时 check/set 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 设置为绕过,这是为什么,是那个标准,看起来像一个安全问题。 (这可能是由安装巧克力引起的,不确定)

来自相应版本/版本的 PowerShell extension for Visual Code respects the persistent execution policy settings[1] of whatever PowerShell version / edition it is configured to use (see ); to manage these settings, use Set-ExecutionPolicy

  • 例如,将当前用户的策略永久设置为RemoteSigned、运行
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

如果你想覆盖在Visual Studio代码中持久配置的策略,在$PROFILE文件中添加Set-ExecutionPolicy命令由 PowerShell 扩展使用:

  • 从 PowerShell 集成控制台,执行 psedit $PROFILE 打开文件进行编辑。
  • Set-ExecutionPolicy -Scope Process RemoteSigned 添加到文件。

至于你的问题

由于HKEY_CLASSES_ROOTHKEY_CURRENT_USER\Software\ClassesHKEY_LOCAL_MACHINE\Software\Classes的复合视图,在这种情况下只有一个条目,您可以使用任一密钥路径访问。

但是,此条目 控制 Windows PowerShell 的持久执行策略;相反,它是一个方便的 context-menu 命令定义,允许您直接从文件资源管理器或桌面执行 *.ps1 文件。作为一项安全功能,该命令确保 仅对正在创建的进程有效的执行策略 (-Scope Process) 至少与 RemoteSigned 一样严格。


[1] 可选阅读:PowerShell 版本存储其(非 GPO)持久执行策略设置的位置

以下仅适用于Windows,因为执行策略在类 Unix 平台上基本上不受支持。

注:

  • 如您问题所示,执行 Get-ExecutionPolicy -List 列出跨范围生效的所有策略:

    • 更具体范围内的设置 - 如果已定义 - 优先;也就是说,没有说明的最具体的范围 Undefined 是对当前流程有效的范围。

    • 组策略GPO-based settings) - i.e., scopes UserPolicy and MachinePolicy, which cannot be set with Set-ExecutionPolicy - can override the CurrentUser, LocalMachine, and Process scopes; notably, this means that even ad-hoc attempts to override the execution policy via -Scope Process Bypass / the -ExecutionPolicy Bypass CLI参数可能被阻止。

  • 您应该使用 Set-ExecutionPolicy 来更改持久性设置,而不是直接修改这些位置。

PowerShell [Core](版本 6 或更高版本)将设置存储在 .json 文件:

  • 当前用户策略Scope -CurrentUser;文件可能不存在):

    • "$([Environment]::GetFolderPath('MyDocuments'))/powershell/powershell.config.json"
  • 机器策略 (Scope -LocalMachine):

    • "$PSHOME\powershell.config.json"
    • 请注意,文件根目录是 powershell,即使实际的可执行文件根目录是 pwsh:

Windows PowerShell(最高版本 5.1)将设置存储在 注册表(密钥不存在,如果您从未 运行 Set-ExecutionPolicy 或者如果您 运行 Set-ExecutionPolicy 将范围的策略设置为 Undefined):

  • 当前用户策略 (-Scope CurrentUser):HKCU:\SOFTWARE\Microsoft\PowerShell\ShellIds\Microsoft.PowerShell,值ExecutionPolicy

    • 32 位和 64 位 PowerShell 可执行文件看到相同的值,因为 HKCU (HKEY_CURRENT_USERS) 没有特定于位的配置单元。
  • 机器策略-Scope LocalMachine):HKLM:\SOFTWARE\Microsoft\PowerShell\ShellIds\Microsoft.PowerShell,值ExecutionPolicy

    • 警告:32 位和 64 位可执行文件看到不同的值,因为 32 位应用程序有一个单独的 HKEY_LOCAL_MACHINE\Software 配置单元。

我不知道这是否可以帮助更多用户解决这个问题,但是从 VSCode 文档中阅读推荐的方法是在 [=11= 中为 Powershell 添加一个新的配置文件],无需全局更改当前策略或使用扩展。沿着这些线的东西:

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