如果 Wait-Process 超时则执行操作

时间:2021-02-23 09:49:45

标签: powershell

如果一个进程在 X 秒内没有完成,我想执行一些代码(包括终止该进程)。我该怎么做?

以下是我目前的尝试。这里的问题是,即使进程超时,catch 子句中的代码也永远不会执行。

$process = Start-Process cmd -ArgumentList "/k process.bat && exit" -PassThru
try {
    Wait-Process $process.Id -Timeout 3
}
catch {
    "Stopping process"
    Stop-Process $process
}

0 个答案:

没有答案