首页 > 解决方案 > 运行任何 powershell 脚本时出现 Set-ExecutionPolicy 错误

问题描述

运行任何 powershell 脚本时出现以下错误。它只发生在客户端的一台服务器上。我不确定是什么触发了这个命令。

如果我将此注册表项从 RemoteSignedto 更改为 ByPass,错误就会消失。

Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass

例如,我有以下一行的简单脚本。

Read-Host -Prompt "Hit Enter to exit"
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:46
+ if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process  ...
+                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Result from Get-ExecutionPolicy -List

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

标签: powershell

解决方案


你需要Run as Administrator然后尝试Set-ExecutionPolicy..

或者您也可以通过这种方式运行 powershell:

powershell.exe -ExecutionPolicy bypass

或者

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

推荐阅读