使用超时从PowerShell运行程序

mag*_*gol 8 powershell timeout process

我将编写一个运行程序的脚本并等待它完成.但是如果程序没有在指定的时间内完成,我希望程序被杀死.

Ale*_*ger 19

这是一个执行此操作的脚本.有关原始示例,请参阅Windows PowerShell博客.

$p = [diagnostics.process]::start("notepad.exe")
if ( ! $p.WaitForExit(1000) ) 
  { echo "Notepad did not exit after 1000ms"; $p.kill() }
Run Code Online (Sandbox Code Playgroud)

  • 在PowerShell 2.0上,您可以用`$ p = Start-Process Notepad -passthru'替换第一行 (11认同)