我的目标是杀死(或以某种方式正常关闭)由powershell脚本启动的子进程,以便在父进程死后(通常通过敲打脚本结尾或通过崩溃或ctrl + c或任何其他方式,使子进程不继续运行)其他方式)。
我已经尝试了几种方法,但是都没有按预期工作:
# only one line was active at time, all other were commented
start-job -scriptblock { & 'notepad.exe' } # notepad.exe started, script continues to end, notepad.exe keep running
start-job -scriptblock { 'notepad.exe' } # notepad.exe not started, script continues to end
notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
& notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
start-Process -passthru -FilePath notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
# give script some time before ending
Write-Host "Begin of sleep section"
Start-Sleep -Seconds 5
Write-Host "End of sleep section"