Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Remove background job if script stopped in ISE

$
0
0

I’m starting some background jobs on the local computer and wanting to terminate the background jobs if the script execution is stopped in PowerShell ISE. Here’s some sample code with comments for the code I can’t find a way to accomplish:

$job = Start-Job -ScriptBlock { do { Write-Host (Get-Date) Sleep 1 } while($true) } while($job.HasMoreData) { Receive-Job $job } # if ise stop button pressed { Remove-Job $job -Force #} 

Is there a way to detect that the PowerShell ISE’s stop button has been clicked, so that background jobs can be removed? As is, the stop button terminates the script being executed, but the background job continues. Completely closing the PowerShell ISE window does terminate the jobs and their associated background processes, but I want to have the jobs and their processes removed when the stop button is pressed.

submitted by /u/redditam
[link] [comments]

The post Remove background job if script stopped in ISE appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles