开发者

stop looped powershell script and kill remote processes initiated by it

i have a script that kicks off a remote job via invoke-command -as开发者_StackOverflow社区job 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.

Write-Host "Press 'q' to quit"

while(1){
    if([console]::KeyAvailable){
        $key = [console]::ReadKey($true)
        if($key.Key -eq 'Q') {break}
    }
}

Just place the if statement into your loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜