i have a script that kicks off a remote job via invoke-command -asjob on 20 servers to run a legacy command line application. It might take a few hours to run it so I used foreach loop until I have 0 instances of (get-job – state running). Inside the loop I put write-progress to inform user about how many servers still running this process.
All works well but I need to add a small feature now to allow users running this script to stop the script and kill all running remote jobs.
Now I know how to kill them, but I have no idea how to allow user to provide feedback back to my loop. Originally I was thinking about popping up a GUI button using async runspace (like described here http://www.vistax64.com/powershell/16998-howto-create-windows-form-without-stopping-script-processing.html) but it looks like I cannot execute functions on the thread with my script from the UI thread.
Is there a way to allow my users to stop the script and kill remote processes?
Here is an example of breaking out of a loop.
Just place the if statement into your loop.