Skip to content

Invoke-SpectreCommandWithProgress

Description

This function takes a script block as a parameter and executes it while displaying a progress bar. The context and task objects are defined at https://spectreconsole.net/api/spectre.console/progresscontext/. The context requires at least one task to be added for progress to be displayed. The task object is used to update the progress bar by calling the Increment() method or other methods defined in Spectre console https://spectreconsole.net/api/spectre.console/progresstask/.


Examples

EXAMPLE 1

Invoke-SpectreCommandWithProgress -ScriptBlock {
    param (
        $Context
    )
    $task1 = $Context.AddTask("A 4-stage process")
    Start-Sleep -Seconds 1
    $task1.Increment(25)
    Start-Sleep -Seconds 1
    $task1.Increment(25)
    Start-Sleep -Seconds 1
    $task1.Increment(25)
    Start-Sleep -Seconds 1
    $task1.Increment(25)
    Start-Sleep -Seconds 1
}

EXAMPLE 2

Invoke-SpectreCommandWithProgress -ScriptBlock {
    param ( $Context )
    
    $jobs = @()
    $jobs += Add-SpectreJob -Context $Context -JobName "Drawing a picture" -Job (
        Start-Job {
            $progress = 0
            while($progress -lt 100) {
                $progress += 1.5
                Write-Progress -Activity "Processing" -PercentComplete $progress
                Start-Sleep -Milliseconds 50
            }
        }
    )
    $jobs += Add-SpectreJob -Context $Context -JobName "Driving a car" -Job (
        Start-Job {
            $progress = 0
            while($progress -lt 100) {
                $progress += 0.9
                Write-Progress -Activity "Processing" -PercentComplete $progress
                Start-Sleep -Milliseconds 50
            }
        }
    )
    
    Wait-SpectreJobs -Context $Context -Jobs $jobs
}

EXAMPLE 3

$result = Invoke-SpectreCommandWithProgress -ScriptBlock {
    param ( $Context )
    
    $task1 = $Context.AddTask("Task with unknown duration")
    $task1.IsIndeterminate = $true
    Start-Sleep -Seconds 5
    $task1.Value = 100
return "Some result"
}
Write-SpectreHost "Result: $result"

EXAMPLE 4

$result = Invoke-SpectreCommandWithProgress -ScriptBlock {
    param ( $Context )
    
    $job = Add-SpectreJob -Context $Context -JobName "Doing some work" -Job (
        Start-Job {
            Start-Sleep -Seconds 10
            return 1234
        }
    )
    
    Wait-SpectreJobs -Context $Context -Jobs $job -EstimatedDurationSeconds 5
    
    $result = Receive-Job -Job $job.Job
    
    return $result
}
Write-SpectreHost "Result: $result"

Parameters

ScriptBlock

The script block to execute.

TypeRequiredPositionPipelineInput
[ScriptBlock]true1false

Syntax

Invoke-SpectreCommandWithProgress [-ScriptBlock] <ScriptBlock> [<CommonParameters>]