截至2022年8月31日上个月底,我有一个正常运行的PS脚本,它会ping服务器并发送一封带有"一切正常"或"问题!"的电子邮件。在8月31日或9月1日左右,电子邮件停止发送,所以我开始调查。我有用于自动化的Win任务调度器,它的历史记录显示bat文件正在按预期进行处理/运行。
然而,当我尝试以管理员身份独自运行PowerShell脚本时,我首先收到的错误是:
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope.
这是我最初的GetExecutionPolicy-列表:
PS C:WINDOWSsystem32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy RemoteSigned
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
因此,我尝试通过cmd行、regedit和gpedit设置ExecutionPolicy。
当我在PS中运行Set ExecutionPolicy-ExecutionPolicy Bypass时,我得到以下错误:
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
+ ... -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
+ FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
然后我运行GetExecutionPolicy-List:
PS C:WINDOWSsystem32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy RemoteSigned
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine ByPass
当我现在尝试运行脚本时,PowerShell会打开,但在不运行脚本的情况下永远挂起。这曾经是有效的,我不知道发生了什么事情使它不起作用。
许多网站对围绕ExecutionPolicy需要做什么有不同的意见。我需要做些什么才能再次工作?最终要求是什么?老实说,我宁愿不使用PowerShell,因为它似乎总是有这些"权限"问题。
这也可能是一个问题:https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/deprecation-of-basic-authentication-exchange-online
但这篇文章似乎并没有提供一个明确的"解决方案"。
非常感谢您的帮助!
更新
谢谢@mktlement0,所以我运行了你的Set。。。我的执行政策如下所示。这是正确的吗?
Scope ExecutionPolicy
----- ---------------
MachinePolicy Bypass
UserPolicy Undefined
Process Bypass
CurrentUser Undefined
LocalMachine RemoteSigned
错误消息试图告诉我们的是,虽然为请求的作用域设置了执行策略,但在优先级较高的作用域中设置的一个策略会覆盖它。
您有一个基于GPO的MachinePolicy
集,它覆盖了所有其他作用域,并使任何调用Set-ExecutionPolicy
或PowerShell CLI的-ExecutionPolicy
参数的尝试无效:计算机上的所有代码都将在策略RemoteSigned
有效的情况下运行。
为了允许Set-ExecutionPolicy
/-ExecutionPolicy
控制有效的执行策略,必须在任何一个基于GPO的作用域中都不设置任何策略(即,Get-ExecutionPolicy
-List
应该为MachinePolicy
和UserPolicy
作用域显示Undefined
(。
如果没有基于GPO的策略,当从外部(例如从任务调度程序(调用PowerShell时,通常会通过PowerShell CLI的-ExecutionPolicy
参数(例如:
powershell.exe -NoProfile -ExecutionPolicy ByPass -File someScript.ps1
也就是说,命令行上的-ExecutionPolicy ByPass
相当于从PowerShell会话内部调用Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
。
另请参阅:
- 概念性about_Execution_Policies帮助主题