|
10 | 10 |
|
11 | 11 | </div>
|
12 | 12 |
|
13 |
| -PSParallelPipeline is a PowerShell Module that includes the `Invoke-Parallel` function, a function that allows parallel processing of input objects with similar capabilities as [`ForEach-Object`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.3) with its `-Parallel` parameter, introduced in PowerShell 7.0. |
| 13 | +PSParallelPipeline is a PowerShell Module that includes `Invoke-Parallel`, a cmdlet that allows for parallel processing of input objects, sharing similar capabilities as |
| 14 | +[`ForEach-Object -Parallel`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object) introduced in PowerShell v7.0. |
14 | 15 |
|
15 |
| -This project is greatly inspired by RamblingCookieMonster's [`Invoke-Parallel`](https://github.com/RamblingCookieMonster/Invoke-Parallel) and Boe Prox's [`PoshRSJob`](https://github.com/proxb/PoshRSJob). |
| 16 | +This project was inspired by RamblingCookieMonster's [`Invoke-Parallel`](https://github.com/RamblingCookieMonster/Invoke-Parallel) and is developed with Windows PowerShell 5.1 users in mind where the closest there is to parallel pipeline processing is [`Start-ThreadJob`](https://learn.microsoft.com/en-us/powershell/module/threadjob/start-threadjob?view=powershell-7.4). |
| 17 | + |
| 18 | +## What does this Module have to offer? |
| 19 | + |
| 20 | +Except for [`-AsJob`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4#-asjob), this module offers the same capabilities as `ForEach-Object -Parallel` in addition to supporting [Common Parameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters), missing feature in the _built-in_ cmdlet. |
| 21 | + |
| 22 | +### Truly pipeline streaming capabilities |
| 23 | + |
| 24 | +```powershell |
| 25 | +Measure-Command { |
| 26 | + $null | Invoke-Parallel { 0..10 | ForEach-Object { Start-Sleep 1; $_ } } | |
| 27 | + Select-Object -First 1 |
| 28 | +} | Select-Object TotalSeconds |
| 29 | +
|
| 30 | +# TotalSeconds |
| 31 | +# ------------ |
| 32 | +# 1.06 |
| 33 | +``` |
| 34 | + |
| 35 | +### Support for [CommonParameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4) |
| 36 | + |
| 37 | +This is something missing on `ForEach-Object -Parallel` as of `v7.5.0.3`. |
| 38 | + |
| 39 | +```powershell |
| 40 | +PS \> 0..5 | ForEach-Object -Parallel { Write-Error $_ } -ErrorAction Stop |
| 41 | +# ForEach-Object: The following common parameters are not currently supported in the Parallel parameter set: |
| 42 | +# ErrorAction, WarningAction, InformationAction, PipelineVariable |
| 43 | +``` |
| 44 | + |
| 45 | +A few examples, they should all work properly, please submit an issue if not 😅. |
| 46 | + |
| 47 | +```powershell |
| 48 | +PS \> 0..5 | Invoke-Parallel { Write-Error $_ } -ErrorAction Stop |
| 49 | +# Invoke-Parallel: 0 |
| 50 | +
|
| 51 | +PS \> 0..5 | Invoke-Parallel { Write-Warning $_ } -WarningAction Stop |
| 52 | +# WARNING: 1 |
| 53 | +# Invoke-Parallel: The running command stopped because the preference variable "WarningPreference" or common parameter is set to Stop: 1 |
| 54 | +
|
| 55 | +PS \> 0..5 | Invoke-Parallel { $_ } -PipelineVariable pipe | ForEach-Object { "[$pipe]" } |
| 56 | +# [0] |
| 57 | +# [1] |
| 58 | +# [5] |
| 59 | +# [2] |
| 60 | +# [3] |
| 61 | +# [4] |
| 62 | +``` |
| 63 | + |
| 64 | +## Improved `-TimeOutSeconds` error message |
| 65 | + |
| 66 | +In `ForEach-Object -Parallel` we get an error message per stopped parallel invocation instead of a single one. |
| 67 | + |
| 68 | +```powershell |
| 69 | +PS \> 0..10 | ForEach-Object -Parallel { $_; Start-Sleep 5 } -TimeoutSeconds 2 |
| 70 | +# 0 |
| 71 | +# 1 |
| 72 | +# 2 |
| 73 | +# 3 |
| 74 | +# 4 |
| 75 | +# InvalidOperation: The pipeline has been stopped. |
| 76 | +# InvalidOperation: The pipeline has been stopped. |
| 77 | +# InvalidOperation: The pipeline has been stopped. |
| 78 | +# InvalidOperation: The pipeline has been stopped. |
| 79 | +# InvalidOperation: The pipeline has been stopped. |
| 80 | +``` |
| 81 | + |
| 82 | +With `Invoke-Parallel` you get a single, _friendlier_, error message. |
| 83 | + |
| 84 | +```powershell |
| 85 | +PS \> 0..10 | Invoke-Parallel { $_; Start-Sleep 5 } -TimeoutSeconds 2 |
| 86 | +# 0 |
| 87 | +# 1 |
| 88 | +# 2 |
| 89 | +# 3 |
| 90 | +# 4 |
| 91 | +# Invoke-Parallel: Timeout has been reached. |
| 92 | +``` |
| 93 | + |
| 94 | +### [`$using:`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_keywords?view=powershell-7.4) Support |
| 95 | + |
| 96 | +Same as `ForEach-Object -Parallel` you can use the `$using:` scope modifier to pass-in variables to the parallel invocations. |
| 97 | + |
| 98 | +```powershell |
| 99 | +$message = 'world!' |
| 100 | +'hello ' | Invoke-Parallel { $_ + $using:message } |
| 101 | +# hello world! |
| 102 | +``` |
| 103 | + |
| 104 | +### `-Functions` and `-Variables` Parameters |
| 105 | + |
| 106 | +Both parameters are a quality of life addition, specially `-Functions`, which adds the locally defined functions to the runspaces [Initial Session State](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate), a missing feature on `ForEach-Object -Parallel`. This is a much better alternative to passing-in the function definition to the parallel scope. |
| 107 | + |
| 108 | +#### [`-Variables` Parameter](./docs/en-US/Invoke-Parallel.md#-variables) |
| 109 | + |
| 110 | +```powershell |
| 111 | +'hello ' | Invoke-Parallel { $_ + $msg } -Variables @{ msg = 'world!' } |
| 112 | +# hello world! |
| 113 | +``` |
| 114 | + |
| 115 | +#### [`-Functions` Parameter](./docs/en-US/Invoke-Parallel.md#-functions) |
| 116 | + |
| 117 | +```powershell |
| 118 | +function Get-Message {param($MyParam) $MyParam + 'world!' } |
| 119 | +'hello ' | Invoke-Parallel { Get-Message $_ } -Functions Get-Message |
| 120 | +# hello world! |
| 121 | +``` |
16 | 122 |
|
17 | 123 | ## Documentation
|
18 | 124 |
|
19 |
| -Check out [__the docs__](./docs/en-US/) for information about how to use this Module. |
| 125 | +Check out [__the docs__](./docs/en-US/Invoke-Parallel.md) for information about how to use this Module. |
20 | 126 |
|
21 | 127 | ## Installation
|
22 | 128 |
|
|
0 commit comments