Enables parallel processing of pipeline input objects.
Invoke-Parallel -InputObject <Object> [-ScriptBlock] <ScriptBlock> [-ThrottleLimit <Int32>]
[-Variables <Hashtable>] [-Functions <String[]>] [-UseNewRunspace] [-TimeoutSeconds <Int32>]
[<CommonParameters>]
PowerShell function that intends to emulate ForEach-Object -Parallel
for those stuck with Windows PowerShell. This function shares similar usage and capabilities than the ones available in the built-in cmdlet.
This project is greatly inspired by RamblingCookieMonster's Invoke-Parallel
and Boe Prox's PoshRSJob
.
Compatible with Windows PowerShell 5.1 and PowerShell Core 7+.
Install-Module PSParallelPipeline -Scope CurrentUser
$message = 'Hello world from {0}'
0..10 | Invoke-Parallel {
$using:message -f [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
} -ThrottleLimit 3
$message = 'Hello world from {0}'
0..10 | Invoke-Parallel {
$message -f [runspace]::DefaultRunspace.InstanceId
Start-Sleep 3
} -Variables @{ message = $message } -ThrottleLimit 3
$threadSafeDictionary = [System.Collections.Concurrent.ConcurrentDictionary[string,object]]::new()
Get-Process | Invoke-Parallel {
$dict = $using:threadSafeDictionary
$dict.TryAdd($_.ProcessName, $_)
}
$threadSafeDictionary["pwsh"]
$threadSafeDictionary = [System.Collections.Concurrent.ConcurrentDictionary[string,object]]::new()
Get-Process | Invoke-Parallel {
$dict.TryAdd($_.ProcessName, $_)
} -Variables @{ dict = $threadSafeDictionary }
$threadSafeDictionary["pwsh"]
function Greet { param($s) "$s hey there!" }
0..10 | Invoke-Parallel { Greet $_ } -Functions Greet
Specifies the input objects to be processed in the Script Block.
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: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies the number of script blocks that are invoked in parallel.
Input objects are blocked until the running script block count falls below the ThrottleLimit
.
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 5
Accept pipeline input: False
Accept wildcard characters: False
Specifies a hash table of variables to have available in the Script Block (Runspaces).
The hash table Keys
become the Variable Name inside the Script Block.
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Existing functions in the Local Session to have available in the Script Block (Runspaces).
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
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:
Required: False
Position: Named
Default value: False
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.
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters. For more information, see about_CommonParameters.
Contributions are more than welcome, if you wish to contribute, fork this repository and submit a pull request with the changes.