Using Powershell 2.0 in a Windows 7 desktop
I want to create a process to run an ant command, and then wait for that process to finish in several minutes. If time out and the process still running then I want to kill it.
I have wrote some code as below:
$p = Start-Process "cmd.exe" '/c ant execute' -PassThru -RedirectStandardOutput $log_file_path
Wait-Process -id $p.ID -timeout 100
if(!$p.hasExited) {
echo "kill the process"
$p.Kill()
#Stop-Process $p.ID
}
Start-Sleep -s 30
# continue to do other things
Although the process didn't get killed, it's still running even after the Start-Sleep statement get executed.
I also tried with Stop-Process command, as the commented out line indicates, no luck, the process still running.
I may have missed something important, please give a clue.
EDIT :
It turns out there are some child processes still running in the cmd window, kill only the cmd process is not enough.
I finally get the job done with following code:
if(!$p.hasExited) {
echo "kill the process"
taskkill /T /F /PID $p.ID
#$p.Kill()
#Stop-Process $p.ID
}