I'm writing a powershell script, that must start some background processes in order to automate a testing process.
# Start Server
$server=Start-Process -FilePath "server" -PassThru
# Does some testing
# Eventually exit 1 will be called
# Stop Server
Stop-Process -Id $server.Id
In case an abnormal exit occured during my testing process I'm not able to stop all started processes with my script an I'm left with some dangling processes. How can I automatically kill all started processes of my script in case the script is exited?