运行任何 powershell 脚本时出现 Set-ExecutionPolicy 错误

Kus*_*nki 3 powershell

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

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

Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass
Run Code Online (Sandbox Code Playgroud)

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

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

Scope                 ExecutionPolicy
-----                 ---------------
MachinePolicy         RemoteSigned
UserPolicy            RemoteSigned
Process               Undefined
CurrentUser           Undefined
LocalMachine          Unrestricted
Run Code Online (Sandbox Code Playgroud)

小智 6

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

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

powershell.exe -ExecutionPolicy bypass
Run Code Online (Sandbox Code Playgroud)

或者

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)