即使禁止Set-ExecutionPolicy,如何运行PowerShell脚本?

PS的Set-ExecutionPolicy命令被禁止,所以我不能这样运行:

PS> .\script.ps1 (enter) 

我想知道除了“Windows PowerShell ISE”之外是否还有其他方法可以运行ps脚本。

ps我能够使用java的ProccessBuilder运行单个ps命令,但不知道如何运行整个脚本。

谢谢

这是我们用来从java运行powershell脚本的东西(无论exec策略如何都可以工作):

 powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File  

最简单的方法就是gc .\script.ps1 | iex gc .\script.ps1 | iex

这在Powershell中有效,并不关心ExecutionPolicy ,只是确保你对新行很小心。 保持{} s和类似的在同一行,使用; 在需要的地方。

Oisin Grehan在他的博客上有一篇有趣的post ,提供了另一种绕过执行政策的方法。 打开一个shell并运行它:

 function Disable-ExecutionPolicy { ($ctx = $executioncontext.gettype().getfield( "_context","nonpublic,instance").getvalue( $executioncontext)).gettype().getfield( "_authorizationManager","nonpublic,instance").setvalue( $ctx, (new-object System.Management.Automation.AuthorizationManager "Microsoft.PowerShell")) } Disable-ExecutionPolicy 

这将删除默认的主机授权管理器,该管理器允许您从该shell调用脚本。 您必须为打开的每个shell运行此命令,因为执行策略仅在运行它的shell中被覆盖。

15种绕过PowerShell执行策略的方法

我没有最爱 – 我讨厌他们。