重启后第一次运行powershell脚本的执行策略提示

Tor*_*erg 5 powershell executionpolicy powershell-5.0

我有一个 PowerShell 脚本,我想用它来自动化一些事情。我通过右键单击它并选择“使用 PowerShell 运行”来运行它。

我已经Set-ExecutionPolicy Bypass在 32 位和 64 位 PowerShell 中运行,并使用Get-ExecutionPolicy.

尽管如此,每次重新启动后我第一次尝试运行脚本时,我收到以下提示:

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):
Run Code Online (Sandbox Code Playgroud)

如果我只是关闭 PowerShell 窗口并再次运行它,则没有提示。无论我输入Y还是N提示,脚本都会运行。

剧本中没有什么可笑的,只是echo "Test". 同样的事情发生在三台不同的计算机上,Win 8.1、Win 10 和虚拟化的 Win 10。它们都有 PowerShell 5.0。

我究竟做错了什么?我怎样才能摆脱这个提示?

Ben*_*est 1

听起来这可能来自远程计算机,我的猜测是该策略可能设置为RemoteSigned, 或Unrestricted并且该文件来自互联网。在这种情况下,您可以使用文件上的“属性”对话框,使用Unblock-Filecmdlet(运行该命令时强制交互)来取消阻止该文件,或者您可以将脚本内容复制到另一个文件并将其保存在本地。

我相信设置Bypass(或任何其他执行策略)只会使您达到本地或组策略设置的最低策略级别(如果设置了)。例如,如果您有一个组策略将执行策略设置为RemoteSigned,但指定为Bypass,则权限边界将停止于RemoteSigned,并且您将受到该执行策略的限制。