【问题标题】:How do you successfully change execution policy and enable execution of PowerShell scripts您如何成功更改执行策略并启用 PowerShell 脚本的执行
【发布时间】:2015-01-03 10:37:32
【问题描述】:

我在更改 Windows Server 2008+ 操作系统中的执行策略时遇到问题。这是我第一次尝试运行需要资源完全访问权限的脚本,并且在 提升模式 下启动 Powershell 后尝试以下操作:

Set-ExecutionPolicy Unrestricted

但我明白了:

Set-ExecutionPolicy : Windows PowerShell updated your execution policy
successfully, but the setting is overridden by a policy defined at a more
specific scope.  Due to the override, your shell will retain its current
effective execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List"
to view your execution policy settings. For more information please see
"Get-Help Set-ExecutionPolicy".
At line:1 char:1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

虽然我是管理员,但我无法更改执行策略。该怎么办?

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    错误消息表明您尝试通过Set-ExecutionPolicy 定义的设置被另一个范围内的设置覆盖。使用Get-ExecutionPolicy -List 查看哪个范围有哪个设置。

    PS C:\> Get-ExecutionPolicy -List
    
            Scope    ExecutionPolicy
            -----    ---------------
    MachinePolicy          Undefined
       UserPolicy          Undefined
          Process          Undefined
      CurrentUser          Undefined
     LocalMachine       RemoteSigned
    
    PS C:\> Set-ExecutionPolicy Restricted -Scope Process -Force
    PS C:\> Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    Set-ExecutionPolicy : Windows PowerShell updated your execution policy
    successfully, but the setting is overridden by a policy defined at a more
    specific scope.  Due to the override, your shell will retain its current
    effective execution policy of Restricted. Type "Get-ExecutionPolicy -List"
    to view your execution policy settings. ...
    PS C:\> Get-ExecutionPolicy -List
    
            Scope    ExecutionPolicy
            -----    ---------------
    MachinePolicy          Undefined
       UserPolicy          Undefined
          Process         Restricted
      CurrentUser       Unrestricted
     LocalMachine       RemoteSigned
    
    PS C:\> .\test.ps1
    .\test.ps1 : File C:\test.ps1 cannot be loaded because running scripts is
    disabled on this system. ...
    PS C:\> Set-ExecutionPolicy Unestricted -Scope Process -Force
    PS C:\> Set-ExecutionPolicy Restricted -Scope CurrentUser -Force
    Set-ExecutionPolicy : Windows PowerShell updated your execution policy
    successfully, but the setting is overridden by a policy defined at a more
    specific scope.  Due to the override, your shell will retain its current
    effective execution policy of Restricted. Type "Get-ExecutionPolicy -List"
    to view your execution policy settings. ...
    PS C:\> Get-ExecutionPolicy -List
    
            Scope    ExecutionPolicy
            -----    ---------------
    MachinePolicy          Undefined
       UserPolicy          Undefined
          Process       Unrestricted
      CurrentUser         Restricted
     LocalMachine       RemoteSigned
    
    PS C:\> .\test.ps1
    Hello World!

    如您所见,尽管存在错误,但仍定义了两个设置,但更具体的范围 (Process) 中的设置仍然优先,阻止或允许脚本执行。

    由于默认范围是LocalMachine,因此错误可能是由CurrentUserProcess 范围中的设置引起的。但是,更常见的原因是脚本执行是通过组策略(本地或域)配置的。

    本地管理员可以通过gpedit.msc(本地组策略编辑器)修改本地组策略,如this answer 中所述。

    域组策略不能被本地设置/策略取代,必须由域管理员通过域控制器上的gpmc.msc(组策略管理)进行更改。

    对于本地和域策略,该设置可以定义为计算机设置:

    Computer Configuration
    `-Administrative Templates
      `-Windows Components
        `-Windows PowerShell -> Turn on Script Execution
    

    或作为用户设置:

    User Configuration
    `-Administrative Templates
      `-Windows Components
        `-Windows PowerShell -> Turn on Script Execution
    

    前者应用于计算机对象,而后者应用于用户对象。对于本地策略,用户策略和计算机策略之间没有显着差异,因为用户策略会自动应用于计算机上的所有用户。

    一个策略可以具有三种状态之一(如果您分别计算状态 Enabled 可用的 3 个设置,则可以具有五个状态):

    • 未配置:策略不控制 PowerShell 脚本执行。
    • 启用:允许执行 PowerShell 脚本。
      • 只允许签名脚本:只允许执行签名脚本(同Set-ExecutionPolicy AllSigned)。
      • 允许本地脚本和远程签名脚本:允许执行所有本地脚本(签名或未签名)和远程位置的签名脚本(与Set-ExecutionPolicy RemoteSigned 相同)。
      • 允许所有脚本:允许执行本地和远程脚本,无论它们是否已签名(与Set-ExecutionPolicy Unrestricted 相同)。
    • 已禁用:禁止执行 PowerShell 脚本(与 Set-ExecutionPolicy Restricted 相同)。

    通过Set-ExecutionPolicy 所做的更改仅在本地和域策略设置为未配置时生效(执行策略UndefinedMachinePolicyUserPolicy 范围内)。

    【讨论】:

      【解决方案2】:

      问题是 Windows 不允许在Unrestricted 模式下执行所有脚本。实际上,无论您的用户(即使是管理员)的执行策略如何,Local Group Policy 都会优先。

      默认情况下,本地组脚本执行策略是不允许执行脚本的。我们需要改变它!

      更改本地组执行策略

      我们通过Local Group Policy Editor 执行此操作,您可以通过在 Windows 搜索栏中搜索“组策略”来访问它。或者这样做:

      1. 点击Win + r 并输入命令mmc 打开管理控制台。
      2. 转到File -> Add Remove Snap In...
      3. 在左侧窗格中找到 Group Policy Object Editor 并添加它。
      4. 关闭表单。

      然后可以在左侧窗格中展开组编辑器。展开它并导航到Computer Configuration -> Administrative Templates -> Windows Components

      然后到Windows PowerShell

      所以选择Turn on Script Execution。将配置更改为Enabled 并在Execution Policy 中指定Allow all scripts

      点击Ok 确认并关闭管理控制台。

      【讨论】:

      • 如果 Windows Power Shell 不在列表中怎么办?
      • 我找到了答案,你需要从这里下载 Windows Power Shell 的第 4 版:microsoft.com/en-us/download/confirmation.aspx?id=40855
      • 此外,我必须在@diganta-kumar 的答案中安装修补程序
      • 优秀的深入信息。但是,在从扩展和更新卸载并重新安装 VS 2013 的 Nuget 包扩展后,我的问题得到了修复
      • 我还必须运行 Set-ExecutionPolicy "Unrestricted" -Scope CurrentUser -Confirm:$false
      【解决方案3】:

      【讨论】:

      • 请注意,此答案与问题中描述的问题无关。它解决了 Visual Studio 扩展的问题。
      【解决方案4】:

      如果您最近在使用 Visual Studio 2015 时遇到此问题,请检查工具 > 扩展和更新中的 nuget 包管理器是否有任何更新>

      【讨论】:

        【解决方案5】:

        如果 PowerShell ExecutionPolicy 是由域控制器通过组策略设置的,您必须在每次启动后在注册表中将 ExecutionPolicy 重置为“绕过”。我创建了一对启动脚本来自动化这个过程。下面,我描述一下我的过程。

        创建一个名为 %USERPROFILE%\Documents\StartupScripts 的文件夹,然后使用以下代码在其中放置一个名为 ExecutionPolicy.ps1 的 PowerShell 脚本:

        Push-Location
        Set-Location HKLM:\Software\Policies\Microsoft\Windows\PowerShell
        Set-ItemProperty . ExecutionPolicy "Bypass"
        Pop-Location
        

        然后创建一个名为 %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Startup.cmd 的文件并将以下代码放入其中:

        PowerShell -Version 3.0 -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
        PowerShell -Version 3.0 "%USERPROFILE%\Documents\StartupScripts\ExecutionPolicy.ps1" >> "%TEMP%\StartupLog.txt" 2>&1
        

        此脚本将在每次登录开始时运行。

        【讨论】:

        • 我认为这不会绕过组策略。至少对我来说不是。在我的情况下,虽然它在 HKCU 下存在,但 HKLM 下不存在密钥。但是您的脚本仍然没有避免安全性。
        • 这是糟糕的建议。它不仅最多只能暂时缓解症状(组策略会定期重新应用,默认情况下每 90-120 分钟一次),而且通常还意味着您违反了公司政策,这可能会导致法律诉讼。
        【解决方案6】:

        即使@Ansgar Wiechers 的回答不起作用.. 那么您的 MachinePolicy 范围也可能存在问题。所以这个问题可以有一个解决方法是.. 在

        处编辑 ExecutionPolicy 键的注册表值

        HKEY_LOCAL_MACHINE -> 软件 -> 策略 -> Microsoft -> Windows -> 电源外壳

        在尝试了这么多解决方案后,它对我执行 ps 脚本很有用。

        【讨论】:

          【解决方案7】:

          将以下内容添加到名为 psa.cmd 的文件中,并放入包含您的 PATH 的文件夹中:

          POWERSHELL -Command "$enccmd=[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content '%1' | Out-String)));POWERSHELL -EncodedCommand $enccmd"
          

          现在您可以运行任何 powershell 脚本,如下所示:

          psa script.ps1
          

          【讨论】:

          • 很好的解决方案。以本机方式运行脚本的 Visual Studio 2015 有时可以工作,有时会挂起。这每次都有效。谢谢马克
          【解决方案8】:

          上次我跳进这个问题时,下面的代码修复了它。不要忘记以管理员权限运行终端并在执行脚本之前重新运行它。

          Set-ItemProperty -path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell" EnableScripts 1
          

          【讨论】:

            【解决方案9】:

            我刚刚做了:

            Set-ExecutionPolicy Unrestricted -Scope Process -Force
            Set-ExecutionPolicy Unrestricted
            

            成功了

            【讨论】:

              猜你喜欢
              • 2021-10-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-11-11
              • 2019-07-03
              • 1970-01-01
              • 2017-03-05
              • 2019-11-04
              相关资源
              最近更新 更多