如何在不显示窗口的情况下将 powershell 脚本作为后台任务运行? [英] How can I run a powershell script as a background task without displaying a window?
问题描述
我正在创建一个脚本,该脚本将触发性能计数器并将它们存储在 .csv 文件中,当文件变得太大时滚动.从 powershell 提示符或 ISE(我正在运行 v.2/win server 2008)运行脚本可以正常工作,并且文件运行正常.
I'm working on creating a script that will fire off performance counters and store them in a .csv file, rolling over when the file gets too big. Running the script from a powershell prompt or the ISE (I'm running v.2/win server 2008) works just fine, and the files are running correctly.
但是,当我尝试使用以下 cmd 提示行执行命令时(甚至尝试在 cmd 上使用/k 开关),命令提示符会立即关闭并且文件不会运行.
However, when I try to execute the command using the following cmd prompt line (even trying the /k switch on cmd), the command prompt closes immediately and the file is not getting run.
powershell.exe -windowstyle hidden {iis_test.ps1}
我也尝试了以下方法,虽然我看到 Powershell 没有停止(我通过任务管理器监视它,寻找 Powershell.exe 出现),但我没有看到正在创建的文件.
I've also tried the following, and although I see Powershell doesn't stop (I monitor it via task manager, looking for Powershell.exe to show up), I'm not seeing the file being created.
powershell.exe -noexit -windowstyle hidden {iis_test.ps1}
我也没有看到在任何可能的地方(事件查看器、命令提示符窗口本身)发现任何错误.
Nor am I seeing any errors being caught in any of the likely places (event viewer, the command prompt window itself).
有什么想法吗?最终,我的目标是让一个 vb winforms 东西调用这些脚本文件,自然我认为弄清楚命令提示符命令会有所帮助.请注意,此脚本的可移植性非常重要,并且可以在任何 Windows 2008 Server 系统上运行(因此我需要远离需要进一步安装的第三方实体).
Any ideas? Ultimately, my goal is to have a vb winforms thing call these script files, and naturally I thought figuring out the command prompt command would be helpful. Note that it is very important that this script be portable, and run on any Windows 2008 Server system (so I need to stay away from third party entities that would require further installation).
我需要使用 -file 参数来指定要运行的文件.
I needed to use the -file argument to specify a file to run.
推荐答案
从 DOS/CMD shell 试试这个:
try this from a DOS/CMD shell:
powershell.exe -windowstyle hidden -file C:iis_test.ps1
这篇关于如何在不显示窗口的情况下将 powershell 脚本作为后台任务运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!