Want to make windows equivalent for GNU timeout (as a oneliner to embed into batch script) to start program and terminate it if this doea on terminate itself after timeout.
powershell.exe "Start-Process 'ping.exe' -ArgumentList '127.0.0.1', '-t' -NoNewWindow -PassThru | % { $_.WaitForExit(3000) }; If(!$?) { $_.Kill() }"
It only print FALSE after timeout, but ping program continue. What am I doing wrong?
$?
holds the error status for the last command invocation, and $_.WaitForExit(3000)
is not a command.
As evident from the ouput you receive, the WaitForExit()
method returns $false
if the target process didn't exit before the timeout was exceeded, so you'll want to inspect that value instead:
... |% { if(-not $_.WaitForExit(3000)) { $_.Kill() } }