powershellprogress-barstart-jobpowershell-jobs

Add Write-Progress to Get-Job/Wait-Job


I'm using the below code to display the results of PowerShell Jobs with a timeout of 120 seconds. I would like to enhance this code by incorporating Write-Progress (based on number of jobs completed). I tried using this example as a reference, however, when I try to incorporate that code, the progress bar displays briefly after all the jobs are all done already.

    $Jobs = @()
    $ForceStoppedIds = @{}
    
    $Jobs += Get-Job
    $Jobs | Wait-Job -Timeout 120 | Out-Null
    $Jobs | ?{$_.State -eq 'Running'} | Stop-Job -PassThru | %{$ForceStoppedIds[$_.Id] = $true}
    
    foreach ($Job in $Jobs) {
    
        $Name = $Job.Name
        $Output = (Get-Job -Name $Name | Receive-Job)
    
        if ($ForceStoppedIds.Contains($Job.Id)) {
    
            Write-Output "$($Name) - Device unable to process request within 2 minutes"
    
        } else {
    
            Write-Output $Output
    
        }
    
    }

Solution

  • Wait-Job -Timeout 120 will block the thread until the specified timeout or all jobs have completed, hence, is not possible to display progress and wait for them at the same time.

    There are 2 alternatives that I can think of, the first one would be to create a proxy command / proxy function around this cmdlet to extend it's functionality.

    These blogs demonstrate how to do it:

    You can also follow the indications from this helpful answer.


    The other alternative is to define your own function that does a similar work as Wait-Job but, instead of blocking the thread, you can add a loop that will run based on 2 conditions:

    Note, the function below should work in most cases however is purely for demonstration purposes only and should not be relied upon.

    First we define a new function that can be used to display progress as well as wait for our jobs based on a timeout:

    using namespace System.Collections.Generic
    using namespace System.Diagnostics
    using namespace System.Threading
    using namespace System.Management.Automation
    
    function Wait-JobWithProgress {
        [cmdletbinding()]
        param(
            [parameter(Mandatory, ValueFromPipeline)]
            [object[]] $InputObject,
    
            [parameter()]
            [ValidateRange(1, [double]::MaxValue)]
            [double] $TimeOut # In seconds!
        )
    
        begin {
            $jobs = [List[object]]::new()
    
            if ($withTimeOut = $PSBoundParameters.ContainsKey('TimeOut')) {
                $span = [timespan]::FromSeconds($TimeOut)
            }
        }
    
        process {
            $jobs.AddRange($InputObject)
        }
    
        end {
            $timer = [Stopwatch]::StartNew()
            $total = $jobs.Count
            $completed = 0.1
    
            while ($jobs.Count) {
                if ($withTimeOut -and $timer.Elapsed -gt $span) {
                    break
                }
    
                $remaining = $total - $completed
                $average = $timer.Elapsed.TotalSeconds / $completed
                $estimate = [math]::Round($remaining * $average)
                $status = 'Completed Jobs: {0:0} of {1}' -f $completed, $total
                $progress = @{
                    Activity         = 'Waiting for Jobs'
                    PercentComplete  = $completed / $total * 100
                    Status           = $status
                    SecondsRemaining = $estimate
                }
                Write-Progress @progress
    
                $id = [WaitHandle]::WaitAny($jobs.Finished, 200)
                if ($id -eq [WaitHandle]::WaitTimeout) {
                    continue
                }
    
                # output this job
                $jobs[$id]
                # remove this job
                $jobs.RemoveAt($id)
                $completed++
            }
    
            # Stop the jobs not yet Completed and remove them
            $jobs | Stop-Job -PassThru | ForEach-Object {
                Remove-Job -Job $_
                'Job [#{0} - {1}] did not complete on time and was removed.' -f $_.Id, $_.Name
            } | Write-Warning
            Write-Progress @progress -Completed
        }
    }
    

    Then for testing it, we can create a few jobs with a random timer:

    0..10 | ForEach-Object {
        Start-Job {
            Start-Sleep (Get-Random -Minimum 5 -Maximum 15)
            [pscustomobject]@{
                Job    = $using:_
                Result = 'Hello from [Job #{0:D2}]' -f $using:_
            }
        }
    } | Wait-JobWithProgress -TimeOut 10 |
    Receive-Job -AutoRemoveJob -Wait | Format-Table -AutoSize