Skip to content

Instantly share code, notes, and snippets.

@SteloNLD
Last active July 5, 2018 22:03
Show Gist options
  • Save SteloNLD/20a2404ac557fe27d58c472201beeca0 to your computer and use it in GitHub Desktop.
Save SteloNLD/20a2404ac557fe27d58c472201beeca0 to your computer and use it in GitHub Desktop.
Powershell Function 'Watch-Job', see the Progress of the Jobs you are running, kill all when one is failed
<#
.Synopsis
Watch Powershell Job progress.
.DESCRIPTION
This function allows you monitor the progress of the Powershell Jobs you just started,
it wil show a progressbar and the remaining jobs that are still running.
one of the key features is the ability to stop the other Jobs when 1 is failed.
.EXAMPLE
Watch-Job -Job $Jobs
Shows a progress bar indication how many jobs are completed/still running
.EXAMPLE
Watch-Job -Job $Jobs -StopJobOnError
Shows a progress bar indication how many jobs are completed/still running
When a single Job is Failed all other jobs that are still Running wil be Stopped.
.EXAMPLE
Watch-Job -Job $Jobs -RemoveJob
Shows a progress bar indication how many jobs are completed/still running
When a Jobs is finished (Failed, Stopped, Completed, ...) the Job including the data it contained will be removed.
.EXAMPLE
Watch-Job -Job $Jobs -StopJobOnError -RemoveJob
Shows a progress bar indication how many jobs are completed/still running
When a single Job is Failed all other jobs that are still Running wil be Stopped.
When a Jobs is finished (Failed, Stopped, Completed, ...) the Job including the data it contained will be removed.
.NOTES
Created by Sten Lootens at 5-7-2018.
Please contact me at [email protected]
.LINK
https://gist.github.com/SteloNLD/20a2404ac557fe27d58c472201beeca0
#>
Function Watch-Job {
[CmdletBinding()]
[OutputType([System.Management.Automation.Job[]])]
Param(
# Powershell Job(s) to Watch,
# normally the result of the Start/Get-Job command or -AsJob parameter.
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true
)]
[Alias("Jobs")]
[ValidateNotNull()]
[System.Management.Automation.Job[]]$Job,
# When Multiple Jobs are provided in the $Job Parameter and a single Job fails
# This wil wil stop the remaining Jobs that are still running.
[Switch]$StopJobOnError,
# This wil remove the Job when it is finished (Failed, Stopped, Completed, ...)
[Switch]$RemoveJob
)
# Setup the ProgessBar
if (-Not $JobStartCount) {
#Create variable for the current scope (function) that is also avaiable to any child scopes (functions).
$VariableJobStartCount = @{
Name = "JobStartCount"
Option = "AllScope"
Value = ($Job | Measure-Object | Select-Object -ExpandProperty Count)
}; New-Variable @VariableJobStartCount
Write-Progress -PercentComplete 0 -Activity "Watch-Job" -Status "$JobStartCount/$JobStartCount Jobs Left" #-CurrentOperation "Waiting for a Job to Finisch"
}
# Wait for a single job to finisch (Failed, Stopped, Completed, ...)
$Job | Wait-Job -Any | ForEach-Object {
if ($_.State -eq 'Failed') {
# Show a warning about the current 'Failed' Job
Write-Warning "Job $($_.Name) returned with state $($_.State)"
# Stop all other Jobs.
if ($StopJobOnError) {
Write-Warning "A Job has failed, forcibly stopping other Jobs"
$Job | Stop-Job
}
}
Else {
# Show information about the current Job
Write-Verbose "Job $($_.Name) retuned with state $($_.State)"
}
# Remove the Current Job
if ($RemoveJob) { $_ | Remove-Job}
# Wait for further Jobs to complete
$RemainingJobs = $Job | Where-Object Id -ne $_.Id
If ($RemainingJobs) {
Write-Progress -PercentComplete (100-($RemainingJobs.Count)/$JobStartCount*100) -Activity "Watch-Job" -Status "$($RemainingJobs.Count)/$JobStartCount Jobs Left"
Watch-Job -Job $RemainingJobs -StopJobOnError:$StopJobOnError -RemoveJob:$RemoveJob | Out-Null
}
Else {
Write-Progress -Activity "Watch-Job" -Completed
}
}
Return $Job
}
@SteloNLD
Copy link
Author

SteloNLD commented Jul 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment