如果一个进程在 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
}