未调用时Set-ExecutionPolicy错误

时间:2018-12-13 11:40:08

标签: powershell executionpolicy

当我从服务器上的.ps1文件运行任何脚本时,会收到有关Set-ExcutionPolicy成功但被更高范围覆盖的错误。 但是,我正在运行的代码均与执行策略或更改它们无关。知道为什么我会收到此错误吗?

这是在Windows 2012 R2服务器上,其中所有级别的执行策略均设置为远程签名。我在PowerShell V4.0上运行

如果我打开PowerShell或ISE并键入命令,则它会完成而不会显示错误,仅当我尝试从.ps1文件运行脚本时才会发生。

这是错误:

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

1 个答案:

答案 0 :(得分:0)

.ps1文件的“使用PowerShell运行” 上下文菜单项会调用以下命令行:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

它存储在注册表项HKCU\Microsoft.PowerShellScript.1\Shell\0\Command中。由于您已通过组策略定义了执行策略,因此,每当通过脚本上下文菜单运行PowerShell脚本时,在Process范围中设置有冲突的执行策略都会导致您观察到错误。

将注册表中的命令行更改为以下内容:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "%L"

错误将消失。