Skip to content
ConsoleMonster
Cmdlet

Invoke-ConsoleRun

Module
ConsoleMonster
Inputs
System.Management.Automation.PSObject

Runs a script block for every input item, optionally in parallel, with a live view of the run.

Syntax

Invoke-ConsoleRun
[-InputObject <PSObject>]
[-Label <Object>]
[-NoLive]
[-Parallel]
[-ReducedMotion]
[-RetryCount <Int32>]
[-RetryDelaySeconds <Double>]
[-ScriptBlock] <ScriptBlock>
[-ThrottleLimit <Int32>]
[-TimeoutSeconds <Double>]
[-Title <Object>]
[-Unordered]
[<CommonParameters>]
#
Parameter set: All Parameter Sets

Description

In GitHub Actions, Azure Pipelines and GitLab CI, sequential items enclose their work and result in a collapsible log group. Parallel items group each completed result atomically; independently emitted worker output keeps its existing routing. CONSOLEMONSTER_CI=none disables provider groups.

$_ is the item, as in ForEach-Object. While it runs, a live view shows a summary line (done of total, percentage, time left, counts), one cell per item, the running items with their elapsed time and the failures; without an interactive console (redirected output, CI, a job) it prints one line per finished item and a summary instead. The output is the script block's ordinary output, in input order (or as each item finishes with -Unordered); an item's errors are written as error records and mark it as failed. While the live view is showing, ordinary output and native stream records are delivered when it ends. With -Parallel, while the live view is showing, host messages, warnings, information, verbose and debug messages, and errors also appear immediately in its log; Write-Progress appears in an activity list, with independent parent and child activities per worker. Without -Parallel, progress bars from the script block are hidden while the live view is showing. Without -Parallel the items run one at a time in the caller's scope, as with ForEach-Object, so variables set in the script block stay. -Parallel runs up to -ThrottleLimit items at a time in their own runspaces on Windows PowerShell 5.1 and PowerShell 7: variables of the caller are passed with $using:name, and -TimeoutSeconds stops an item that runs too long. -Label sets each item's label: a property name or a script block. -RetryCount runs a failed or timed-out item again (after -RetryDelaySeconds); only the last attempt's output and errors are kept, the item's cell shows the retry and the summary counts retried items. Ctrl+C stops the run: running items are stopped and the rest are counted as skipped.

Examples

Example 1

$servers = @(
    [pscustomobject]@{ Name = 'web01' }
    [pscustomobject]@{ Name = 'web02' }
)
$runOptions = @{
    Parallel = $true
    ThrottleLimit = 8
    Label = 'Name'
    ScriptBlock = { $_.Name.ToUpperInvariant() }
}
$servers | Invoke-ConsoleRun @runOptions

Inspect two records in parallel. The pipeline supplies one record per invocation of ScriptBlock. Label reads Name for the dashboard, while ScriptBlock returns the uppercased name to the PowerShell pipeline. ThrottleLimit is an upper bound, so this example starts at most two workers.

Example 2

$servers = @('web-01', 'web-02', 'web-03')
$runOptions = @{
    Parallel = $true
    ThrottleLimit = 3
    TimeoutSeconds = 3
    ErrorAction = 'SilentlyContinue'
    ErrorVariable = 'failures'
    ScriptBlock = {
        if ($_ -eq 'web-03') { throw "$_ restart check failed" }
        [pscustomobject]@{ Host = $_; Message = "$_ preview passed" }
    }
}
$results = @($servers | Invoke-ConsoleRun @runOptions)
$results
$failures

Separate successful results from error records, as in patch-progress. SilentlyContinue suppresses the error display from the cmdlet's error stream, while ErrorVariable retains the error record. A live dashboard can still show the worker error in its log. The other preview checks return ordinary PowerShell objects. The sample makes no changes to any host.

Example 3

Get-ConsoleDemo -Slug 'patch-progress' | Select-Object -ExpandProperty Source

Read the full patch-progress example, which uses Invoke-ConsoleRun and renders a report from the retained successes and failures.

Example 4

$runOptions = @{
    ScriptBlock = { $_.Name }
}
Get-ChildItem -File | Select-Object -First 5 | Invoke-ConsoleRun @runOptions

Read up to five filenames sequentially. Each invocation sees the current FileInfo as $_ and emits its Name. Without Parallel, the script runs in the caller's scope.

Example 5

$runOptions = @{
    Parallel = $true
    ThrottleLimit = 8
    TimeoutSeconds = 10
    Title = 'Patching web farm'
    Label = { 'web{0:d2}' -f $_ }
    ScriptBlock = {
        Start-Sleep -Milliseconds (Get-Random -Minimum 200 -Maximum 1500)
        if ($_ % 13 -eq 0) { throw ('web{0:d2} did not come back after the reboot' -f $_) }
    }
}
1..40 | Invoke-ConsoleRun @runOptions

Simulate a forty-host patch wave like the parallel-patch gallery demo. Up to eight items run at once with a ten-second limit per item in this example. Label maps each number to a display name. The synthetic failures on items 13, 26 and 39 produce error records and mark those items as failed.

Example 6

$servers = @(
    [pscustomobject]@{ Name = 'server01' }
    [pscustomobject]@{ Name = 'server02' }
)
$runOptions = @{
    Parallel = $true
    RetryCount = 2
    RetryDelaySeconds = 5
    Label = 'Name'
    ScriptBlock = { "Would restart $($_.Name)" }
}
$servers | Invoke-ConsoleRun @runOptions

Configure up to two retries with a five-second delay after a failed attempt. The sample emits a message and succeeds on its first attempt. Use the same settings around a script block that can fail to exercise the retry path.

Parameters

-InputObject PSObject optionalpipeline: True (ByValue)
The items (pipeline input).
-Label Object optional
Each item's label in the view: a property name or a script block ($_ is the item). Default a name-like property, or the item's text.
-NoLive SwitchParameter optional
Disables live redraws and uses append-only output.
-Parallel SwitchParameter optional
Runs items at the same time, each in its own runspace (pass caller variables with $using:name).
-ReducedMotion SwitchParameter optional
Disables animation and limits live redraws to once a second without changing the layout. Accessible mode always enables reduced motion.
-RetryCount Int32 optional
How many more times a failed or timed-out item runs before it counts as failed (default 0).
-RetryDelaySeconds Double optional
Seconds to wait before each retry (default 0).
-ScriptBlock ScriptBlock requiredposition: 0
The script block run for each item ($_ is the item).
-ThrottleLimit Int32 optional
How many items run at the same time with -Parallel (default 5).
-TimeoutSeconds Double optional
Seconds an item may run with -Parallel before it is stopped and counted as failed (default no limit; ignored with a warning without -Parallel).
-Title Object optional
A title above the live view (markup).
-Unordered SwitchParameter optional
Writes each item's output as soon as it finishes instead of in input order (when the live view is not showing).

Common Parameters

This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

For more information, see about_CommonParameters.