我如何运行PowerShell脚本作为后台任务,而不显示一个窗口?(How can I run a

2019-09-01 12:39发布

我正在创建一个脚本,将激发关闭性能计数器,并将它们存储在一个.csv文件,翻身当文件变得太大。 从运行一个PowerShell提示符或ISE脚本(我跑第2版/服务器赢得2008年)工作得很好,并且文件被运行正常。

然而,当我尝试使用以下命令提示符行(甚至还试图在CMD / k开关)来执行命令,在命令提示符下立即关闭该文件没有得到运行。

powershell.exe -windowstyle hidden {iis_test.ps1}

我也试过以下,虽然我看到PowerShell不会停止(我通过任务管理器监视它,寻找Powershell.exe展现出来),我没有看到正在创建的文件。

powershell.exe -noexit -windowstyle hidden {iis_test.ps1}

我也不是看到被卷入任何可能的地方的任何错误(事件查看器,命令提示符窗口)。

有任何想法吗? 最终,我的目标是有一个VB的WinForms东西调用这些脚本文件,自然我以为搞清楚命令提示符下命令将是有益的。 请注意 ,这是很重要的,这个脚本是便携式的,任何的Windows 2008 Server系统上运行(所以我需要从第三方实体,将需要进一步的安装走就走)。

编辑:我需要使用-file参数来指定一个文件来运行。

Answer 1:

从DOS / CMD壳试试这个:

powershell.exe -windowstyle hidden -file C:\iis_test.ps1


Answer 2:

Powershell.exe -windowstyle hidden -file C:\iis_test.ps1

But when you run this command, a CMD window will appear.
However, you can use VBscript instead

Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing


文章来源: How can I run a powershell script as a background task without displaying a window?