【发布时间】:2020-05-28 04:27:54
【问题描述】:
我有一个 Powershell 脚本来放松机器上的执行策略,基本上是在运行:
Set-ExecutionPolicy Unrestricted -Force
由于机器上的 ExecutionPolicy 受到限制,我需要使用 .bat 文件启动 .ps1 脚本,该文件绕过执行策略,如下所示:
PowerShell.exe -ExecutionPolicy Bypass -File ./psscripts/myScript.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 Bypass.
错误试图告诉我当前进程的策略范围(使用 -Bypass 启动)覆盖了我刚刚设置的范围,但我并不关心,所以我想简单地隐藏这个错误。
我尝试过使用 -ErrorAction SilentlyContinue:
Set-ExecutionPolicy Unrestricted -Force -ErrorAction SilentlyContinue
但错误仍然显示。所以我尝试将错误流重定向到 &NULL,如下所示:
Set-ExecutionPolicy Unrestricted -Force 2> $NULL
...但即使这样,错误仍然会在终端上弹出。
我已经成功地使用这样的 try-catch 来消除错误:
try{Set-ExecutionPolicy Unrestricted -Force} catch {}
但是,我仍然想了解为什么其他两种方法不起作用? 尝试将错误流(或 any 流)重定向到变量,变量结果为空,所以我假设我试图以某种方式将流从错误的进程重定向? 这与从 .bat 文件启动 Powershell 有关吗?
谁能帮帮我?
【问题讨论】:
标签: powershell cmd