如何在没有终端窗口的情况下运行 PowerShell 脚本? [英] How to run PowerShell script without terminal window?
问题描述
下面的快捷方式执行powershell.exe并将脚本vscode.ps1传递给它.除了我的所有尝试,它在运行时仍会短暂显示一个终端窗口.你可以看到我已经传递了应该防止这种情况的参数,但我仍然看到了窗口.要在不显示终端窗口的情况下从快捷方式执行 PS 脚本,我该怎么做?
The shortcut below executes powershell.exe and passes it the script vscode.ps1. It works except that for all my attempts, it still displays a terminal window briefly when running. You can see I've passed parameters which is supposed to prevent this, but I still see the window. What do I have to do to execute a PS script from a shortcut without the terminal window being displayed?
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -WindowStyle Hidden -file C:\Users\bernie\OneDrive\PowerShell\vscode.ps1 C:\Users\bernie\OneDrive\Documents\windows.txt
推荐答案
这是一个已知问题,请参阅 https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
It's a known issue, see https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
正如@Ciantic 提到的,解决此问题的最佳方法是使用 VB 脚本:
As @Ciantic mentioned, the best way to work around this issue is by using a VB script:
在,说ps-run.vbs
put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
然后用它来运行你想要的命令,例如
Then use it to run the command you want, e.g.
wscript "C:\Path\To\ps-run.vbs" "C:\Other\Path\To\your-script.ps1"
我使用类似这样的东西来频繁地运行任务而没有看到任何闪烁的窗口.
I use something like this to run a task frequently without seeing any flashing windows.
这篇关于如何在没有终端窗口的情况下运行 PowerShell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!