external help file | Module Name | online version | schema |
---|---|---|---|
PSParallelPipeline-help.xml |
PSParallelPipeline |
2.0.0 |
Parallel processing of pipeline input objects.
Invoke-Parallel
-InputObject <Object>
[-ScriptBlock] <ScriptBlock>
[-ThrottleLimit <Int32>]
[-Variables <Hashtable>]
[-Functions <String[]>]
[-UseNewRunspace]
[-TimeoutSeconds <Int32>]
[<CommonParameters>]
Invoke-Parallel
is a PowerShell cmdlet that allows parallel processing of input objects with similar capabilities as
ForEach-Object -Parallel
introduced in PowerShell v7.0.
$message = 'Hello world from '
0..10 | Invoke-Parallel {
$using:message + [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
}
$message = 'Hello world from '
0..10 | Invoke-Parallel {
$message + [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
} -Variables @{ message = $message }
-Variables
specifies a hashtable with key / value pairs of variables to pass-in to the parallel scope. The hashtable keys defines the name for passed-in variables. This parameter is an alternative for the $using:
scope modifier.
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
Get-Process | Invoke-Parallel { ($using:dict)[$_.Id] = $_ }
$dict[$PID]
$dict = [System.Collections.Concurrent.ConcurrentDictionary[int, object]]::new()
Get-Process | Invoke-Parallel { $dict[$_.Id] = $_ } -Variables @{ dict = $dict }
$dict[$PID]
function Greet { param($s) "$s hey there!" }
0..10 | Invoke-Parallel { Greet $_ } -Functions Greet
-Functions
adds locally defined functions to the runspaces Initial Session State allowing you to use them in the parallel scope.
0..10 | Invoke-Parallel { Start-Sleep 1 } -TimeoutSeconds 3
All parallel invocations are stopped when the timeout is reached and any remaining input objects to be processed are ignored.
0..3 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId } -ThrottleLimit 2
# Guid
# ----
# c945ae1f-4e66-4312-b23c-f3994965308e
# 1c6af45c-8727-4488-937a-4dfc1d259e9e
# c945ae1f-4e66-4312-b23c-f3994965308e
# 1c6af45c-8727-4488-937a-4dfc1d259e9e
0..3 | Invoke-Parallel { [runspace]::DefaultRunspace.InstanceId } -ThrottleLimit 2 -UseNewRunspace
# Guid
# ----
# 7a1c3871-6ce2-4b7f-ae90-fb1e92cd9678
# 2488be9e-15fe-4be2-882d-7d98b068c913
# d3dd7b5d-e7e3-457f-b6fb-def35fe837d7
# 9af7c222-061d-4c89-b073-375ee925e538
By default the runspaces are reused. With -UseNewRunspace
a new runspace is created per input object.
Specifies existing functions in the Local Session to have added to the runspaces Initial Session State.
Tip
This method is the recommended way of passing-in local functions to the parallel scope. The alternative to this method is passing-in the function definition (as a string) to the parallel scope and define the function in it.
Type: String[]
Parameter Sets: (All)
Aliases: funcs
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies the input objects to be processed in the ScriptBlock.
Note
This parameter is intended to be bound from pipeline.
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
Specifies the operation that is performed on each input object. This script block is run for every object in the pipeline.
Type: ScriptBlock
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies the number of script blocks that are invoked in parallel (Degree of Parallelism). Input objects are blocked until the running script block count falls below the ThrottleLimit.
Note
-ThrottleLimit
default value is 5
.
Type: Int32
Parameter Sets: (All)
Aliases: tl
Required: False
Position: Named
Default value: 5
Accept pipeline input: False
Accept wildcard characters: False
Specifies the number of seconds to wait for all input to be processed in parallel. After the specified timeout time, all running scripts are stopped and any remaining input objects to be processed are ignored.
Note
Default value of 0
disables the timeout and the cmdlet runs until all pipeline input is processed.
Type: Int32
Parameter Sets: (All)
Aliases: to
Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: False
Uses a new runspace for each parallel invocation instead of reusing them.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: unr
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
Specifies a hashtable of variables to have available in the parallel scope. The hashtable keys defines the name for passed-in variables.
Tip
This parameter is an alternative for the $using:
scope modifier.
Type: Hashtable
Parameter Sets: (All)
Aliases: vars
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters. For more information, see about_CommonParameters.
You can pipe any object to this cmdlet.
This cmdlet returns objects that are determined by the script block.