如何抑制工作流执行时弹出的PowerShell蓝色横幅?
问题:抑制PowerShell工作流调用时的蓝色横幅
在PowerShell脚本中运行工作流时,调用工作流会弹出蓝色横幅窗口,暂时遮挡脚本的有用输出。测试脚本如下:
workflow foobar { Param ($MountedDrives) powershell -command C:\users\admin\desktop\Foobar2.ps1 $MountedDrives } $mountedDrives="D:" foobar $mountedDrives
已尝试以下两种方式,但均无法抑制该横幅:
方式1:
workflow foobar { powershell -windowstyle hidden -command foobar2.ps1 }
方式2:
workflow foobar { powershell -nologo -command foobar2.ps1 }
请问是否有办法彻底抑制这个蓝色横幅?
解决方案
蓝色横幅来自你在工作流中额外启动的新PowerShell进程,以下几种方法可以彻底解决这个问题:
1. 直接调用脚本(推荐)
工作流本身运行在PowerShell环境中,无需额外启动powershell.exe,直接用&操作符执行脚本即可,不会产生新窗口:
workflow foobar { Param ($MountedDrives) & C:\users\admin\desktop\Foobar2.ps1 $MountedDrives } $mountedDrives="D:" foobar $mountedDrives
2. 必须启动新进程时的参数组合
若因特殊需求必须启动新进程,需同时使用-WindowStyle Hidden和-NoInteractive参数,且注意路径与参数的引号包裹:
workflow foobar { Param ($MountedDrives) powershell -NoInteractive -WindowStyle Hidden -Command "& 'C:\users\admin\desktop\Foobar2.ps1' '$MountedDrives'" } $mountedDrives="D:" foobar $mountedDrives
3. 使用Invoke-Command执行脚本
在工作流中通过Invoke-Command执行脚本,默认不会弹出新窗口,需用$using:关键字引用外部变量:
workflow foobar { Param ($MountedDrives) Invoke-Command -ScriptBlock { & C:\users\admin\desktop\Foobar2.ps1 $using:MountedDrives } } $mountedDrives="D:" foobar $mountedDrives
内容的提问来源于stack exchange,提问作者Lando
相关产品推荐
相关产品推荐

