【问题标题】:VSCode overrides PowerShell ExecutionPolicyVSCode 覆盖 PowerShell ExecutionPolicy
【发布时间】:2020-04-13 06:55:06
【问题描述】:

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

        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 ,这是为什么,是标准,看起来像一个安全问题。 (这可能是由于安装巧克力造成的,不确定)

【问题讨论】:

  • 认为设置 ExecutionPolicy 的 2 个注册表项没有指向任何可以正常启动的 shell。在启动它们中的任何一个时,它们不会影响控制台外壳中的 ExecutionPolicy。不是 v5 控制台和 ISE ,也不是 v7 ,也不是 x86 或 x64 ......他们指向什么 shell,为什么这些条目在那里?
  • 为进程设置执行策略并没有帮助,因为重新启动 VSCode 会将其设置回绕过。 Set-ExecutionPolicy -Scope Process -ExecutionPolicy Undefined -Force -Verbose

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    Visual CodePowerShell extension 尊重其配置为使用的任何 PowerShell 版本/版本的持久执行策略设置[1](请参阅 this answer);要管理这些设置,请使用相应版本/版次中的Set-ExecutionPolicy

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

    如果您想覆盖 Visual Studio Code 中持久配置的策略,请将Set-ExecutionPolicy 命令添加到$PROFILE 文件中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 的设置) - 即范围UserPolicyMachinePolicy,不能使用Set-ExecutionPolicy 设置 - 可以覆盖CurrentUserLocalMachineProcess范围;值得注意的是,这意味着即使是通过 -Scope Process Bypass / -ExecutionPolicy Bypass CLI 参数覆盖执行策略的临时尝试也可能会被阻止。

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

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

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

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

      • "$PSHOME\powershell.config.json"
      • 请注意,文件名 root 是 powershell,尽管实际的可执行文件名 root 是 pwsh

    Windows PowerShell(最高 5.1 版)将设置存储在 注册表 中(如果您从未运行过 Set-ExecutionPolicy 或如果您运行Set-ExecutionPolicy 将作用域的策略设置为Undefined):

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 2018-06-29
      • 2011-07-08
      • 2021-06-26
      • 2021-11-30
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多