如何成功更改执行策略并启用PowerShell脚本的执行

And*_*dry 41 windows powershell

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

Set-ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)

但我明白了:

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
Run Code Online (Sandbox Code Playgroud)

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

Ans*_*ers 35

错误消息表示您尝试定义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!
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

Computer Configuration
`-Administrative Templates
  `-Windows Components
    `-Windows PowerShell -> Turn on Script Execution
Run Code Online (Sandbox Code Playgroud)

或作为用户设置:

User Configuration
`-Administrative Templates
  `-Windows Components
    `-Windows PowerShell -> Turn on Script Execution
Run Code Online (Sandbox Code Playgroud)

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

策略可以具有三种状态之一(如果计算可用于单独启用状态的3种设置,则为五种状态):

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

Set-ExecutionPolicy仅当本地和域策略设置为" 未配置"(Undefined范围MachinePolicy和中的执行策略UserPolicy)时,所做的更改才会生效.


And*_*dry 33

问题是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,并指定Allow all scriptsExecution Policy.

在此输入图像描述

通过按下Ok并关闭管理控制台进行确认.

  • 如果Windows Power Shell不在列表中,您会怎么做? (4认同)
  • 我找到了答案,您需要从这里下载 Windows Power Shell 版本 4:http://www.microsoft.com/en-us/download/confirmation.aspx?id=40855 (2认同)

Dig*_*mar 10

现在可以安装修补程序:

2.8.7 for VS 2013:https: //github.com/NuGet/Home/releases/download/2.8.7/NuGet.Tools.vsix

3.1.1 for VS 2015:https: //github.com/NuGet/Home/releases/download/3.1.1/NuGet.Tools.vsix

https://github.com/NuGet/Home/issues/974

  • 请注意,此答案与问题中描述的问题无关。它解决了Visual Studio扩展的问题。 (3认同)

Jha*_*118 8

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


Ras*_*ain 7

即使@Ansgar Wiechers 的回答不起作用.. 那么您的 MachinePolicy 范围可能会出现问题。因此,该问题的一种解决方法是.. 编辑 ExecutionPolicy 键的注册表值在

HKEY_LOCAL_MACHINE -> 软件 -> 策略 -> Microsoft -> Windows -> Powershell

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


Per*_*let 6

如果域控制器通过组策略设置了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
Run Code Online (Sandbox Code Playgroud)

然后创建一个名为%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
Run Code Online (Sandbox Code Playgroud)

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

  • 这是*糟糕的*建议。它最多只能暂时缓解症状(定期重新应用组策略,默认情况下每 90-120 分钟一次),而且通常还意味着您违反了公司策略,这可能会导致法律诉讼。 (2认同)

Mar*_*arc 6

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

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

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

psa script.ps1
Run Code Online (Sandbox Code Playgroud)