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/ .
See https://spectreconsole.net/live/progress for more information.
Examples
Example 1
This example demonstrates a 4-stage process with one loading bar that loads in four chunks.
Invoke-SpectreCommandWithProgress - ScriptBlock {
[ Spectre.Console.ProgressContext ] $Context
# AddTask() returns a https://spectreconsole.net/api/spectre.console/progresstask/ object
$task1 = $Context .AddTask ( " A 4-stage process " )
Example 2
This example demonstrates a 2-stage process with two loading bars running in parallel.
Invoke-SpectreCommandWithProgress - ScriptBlock {
[ Spectre.Console.ProgressContext ] $Context
$jobs += Add-SpectreJob - Context $Context - JobName " Drawing a picture " - Job (
while ( $progress -lt 100 ) {
Write-Progress - Activity " Processing " - PercentComplete $progress
Start-Sleep - Milliseconds 50
$jobs += Add-SpectreJob - Context $Context - JobName " Driving a car " - Job (
while ( $progress -lt 100 ) {
Write-Progress - Activity " Processing " - PercentComplete $progress
Start-Sleep - Milliseconds 50
Wait-SpectreJobs - Context $Context - Jobs $jobs
Example 3
This example demonstrates a task with an unknown (indeterminate) duration.
$result = Invoke-SpectreCommandWithProgress - ScriptBlock {
[ Spectre.Console.ProgressContext ] $Context
$task1 = $Context .AddTask ( " Task with unknown duration " )
$task1 .IsIndeterminate = $true
Write-SpectreHost " Result: $result "
Example 4
This example demonstrates a job with an estimated duration, after the estimated duration has passed the job will switch to an indeterminate state.
$result = Invoke-SpectreCommandWithProgress - ScriptBlock {
[ Spectre.Console.ProgressContext ] $Context
$job = Add-SpectreJob - Context $Context - JobName " Doing some work " - Job (
Wait-SpectreJobs - Context $Context - Jobs $job - EstimatedDurationSeconds 5
$result = Receive-Job - Job $job .Job
Write-SpectreHost " Result: $result "
Parameters
ScriptBlock
The script block to execute.
Type Required Position PipelineInput [ScriptBlock]
true 1 false
Syntax
Invoke-SpectreCommandWithProgress [ -ScriptBlock ] < ScriptBlock > [ < CommonParameters > ]