Skip to content

Commit da248e5

Browse files
authored
Merge pull request #2 from santisq/dev
Enhacements
2 parents 06532b0 + 355e47a commit da248e5

File tree

5 files changed

+91
-55
lines changed

5 files changed

+91
-55
lines changed

.github/workflows/publish.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Publish Workflow
2+
on:
3+
release:
4+
types:
5+
- published
6+
7+
env:
8+
DOTNET_CLI_TELEMETRY_OPTOUT: 1
9+
POWERSHELL_TELEMETRY_OPTOUT: 1
10+
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
11+
DOTNET_NOLOGO: true
12+
13+
jobs:
14+
publish:
15+
name: publish
16+
runs-on: windows-latest
17+
if: startsWith(github.ref, 'refs/tags/v') && github.event_name == 'release'
18+
steps:
19+
- uses: actions/checkout@v3
20+
21+
- name: Publish to Gallery
22+
shell: pwsh
23+
run: >-
24+
Publish-Module $PSScriptRoot -NuGetApiKey $env:PSGALLERY_TOKEN
25+
env:
26+
PSGALLERY_TOKEN: ${{ secrets.PSGALLERY_TOKEN }}

PSParallelPipeline.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSParallelPipeline.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.0'
15+
ModuleVersion = '1.0.1'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -78,7 +78,7 @@ CmdletsToExport = @()
7878
VariablesToExport = @()
7979

8080
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
81-
AliasesToExport = @('parallel')
81+
AliasesToExport = @('parallel', 'parallelpipeline')
8282

8383
# DSC resources to export from this module
8484
# DscResourcesToExport = @()
@@ -95,7 +95,7 @@ PrivateData = @{
9595
PSData = @{
9696

9797
# Tags applied to this module. These help with module discovery in online galleries.
98-
Tags = @('parallel', 'concurrency')
98+
Tags = @('parallel', 'concurrency', 'runspace', 'parallel-processing')
9999

100100
# A URL to the license for this module.
101101
LicenseUri = 'https://github.com/santisq/PSParallelPipeline/blob/main/LICENSE'

PSParallelPipeline.psm1

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ function Invoke-Parallel {
1010
Enables parallel processing of pipeline input objects.
1111
1212
.DESCRIPTION
13-
PowerShell function that intends to emulate `ForEach-Object -Parallel` functionality in PowerShell Core for those stuck with Windows PowerShell.
13+
PowerShell function that intends to emulate [`ForEach-Object -Parallel`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2#-parallel) for those stuck with Windows PowerShell.
1414
This function shares similar usage and capabilities than the ones available in the built-in cmdlet.
15-
This was inspired by RamblingCookieMonster's [`Invoke-Parallel`](https://github.com/RamblingCookieMonster/Invoke-Parallel) and Boe Prox's [`PoshRSJob`](https://github.com/proxb/PoshRSJob)
16-
and is merely a simplified take on those.
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) and is merely a simplified take on those with some few improvements.
1716
1817
TO DO:
1918
- Add `-TimeoutSeconds` parameter.
@@ -31,17 +30,19 @@ function Invoke-Parallel {
3130
Input objects are blocked until the running script block count falls below the ThrottleLimit.
3231
The default value is `5`.
3332
34-
.PARAMETER ArgumentList
35-
Specifies a hashtable of values to be passed to the Runspaces.
36-
Hashtable Keys become the Variable Name inside the ScriptBlock.
33+
.PARAMETER Variables
34+
Specifies a hash table of variables to have available in the Script Block (Runspaces).
35+
The hash table `Keys` become the Variable Name inside the Script Block.
36+
37+
.PARAMETER Functions
38+
Existing functions in the Local Session to have available in the Script Block (Runspaces).
3739
3840
.PARAMETER ThreadOptions
3941
These options control whether a new thread is created when a command is executed within a Runspace.
40-
See [`PSThreadOptions` Enum](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.psthreadoptions?view=powershellsdk-7.2.0).
42+
This parameter is limited to `ReuseThread` and `UseNewThread`.
4143
Default value is `ReuseThread`.
44+
See [`PSThreadOptions` Enum](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.psthreadoptions?view=powershellsdk-7.2.0) for details.
4245
43-
.PARAMETER Functions
44-
Existing functions in the current scope that we want to have available in the Runspaces.
4546
4647
.EXAMPLE
4748
$message = 'Hello world from {0}'
@@ -59,9 +60,9 @@ function Invoke-Parallel {
5960
0..10 | Invoke-Parallel {
6061
$message -f [runspace]::DefaultRunspace.InstanceId
6162
Start-Sleep 3
62-
} -ArgumentList @{ message = $message } -ThrottleLimit 3
63+
} -Variables @{ message = $message } -ThrottleLimit 3
6364
64-
Same as Example 1 but with `-ArgumentList` parameter.
65+
Same as Example 1 but with `-Variables` parameter.
6566
6667
.EXAMPLE
6768
$sync = [hashtable]::Synchronized(@{})
@@ -80,11 +81,11 @@ function Invoke-Parallel {
8081
8182
Get-Process | Invoke-Parallel {
8283
$sync[$_.Name] += @( $_ )
83-
} -ArgumentList @{ sync = $sync }
84+
} -Variables @{ sync = $sync }
8485
8586
$sync
8687
87-
Same as Example 3, but using `-ArgumentList` to pass the reference instance to the runspaces.
88+
Same as Example 3, but using `-Variables` to pass the reference instance to the runspaces.
8889
This method is the recommended when passing reference instances to the runspaces, `$using:` may fail in some situations.
8990
9091
.EXAMPLE
@@ -101,7 +102,7 @@ function Invoke-Parallel {
101102
#>
102103

103104
[CmdletBinding(PositionalBinding = $false)]
104-
[Alias('parallel')]
105+
[Alias('parallel', 'parallelpipeline')]
105106
param(
106107
[Parameter(Mandatory, ValueFromPipeline)]
107108
[object] $InputObject,
@@ -113,10 +114,7 @@ function Invoke-Parallel {
113114
[int] $ThrottleLimit = 5,
114115

115116
[Parameter()]
116-
[hashtable] $ArgumentList,
117-
118-
[Parameter()]
119-
[PSThreadOptions] $ThreadOptions = [PSThreadOptions]::ReuseThread,
117+
[hashtable] $Variables,
120118

121119
[Parameter()]
122120
[ArgumentCompleter({
@@ -128,15 +126,19 @@ function Invoke-Parallel {
128126

129127
(Get-Command -CommandType Filter, Function).Name -like "$wordToComplete*"
130128
})]
131-
[string[]] $Functions
129+
[string[]] $Functions,
130+
131+
[Parameter()]
132+
[ValidateSet('ReuseThread', 'UseNewThread')]
133+
[PSThreadOptions] $ThreadOptions = [PSThreadOptions]::ReuseThread
132134
)
133135

134136
begin {
135137
try {
136138
$iss = [initialsessionstate]::CreateDefault2()
137139

138-
foreach($key in $ArgumentList.PSBase.Keys) {
139-
$iss.Variables.Add([SessionStateVariableEntry]::new($key, $ArgumentList[$key], ''))
140+
foreach($key in $Variables.PSBase.Keys) {
141+
$iss.Variables.Add([SessionStateVariableEntry]::new($key, $Variables[$key], ''))
140142
}
141143

142144
foreach($function in $Functions) {
@@ -150,9 +152,11 @@ function Invoke-Parallel {
150152
$varText = $usingstatement.Extent.Text
151153
$varPath = $usingstatement.SubExpression.VariablePath.UserPath
152154

153-
if(-not $usingParams.ContainsKey($varText)) {
154-
$key = [Convert]::ToBase64String([Encoding]::Unicode.GetBytes($varText))
155-
$usingParams[$key] = $ExecutionContext.SessionState.PSVariable.Get($varPath).Value
155+
# Credits to mklement0 for catching up a bug here. Thank you!
156+
# https://github.com/mklement0
157+
$key = [Convert]::ToBase64String([Encoding]::Unicode.GetBytes($varText))
158+
if(-not $usingParams.ContainsKey($key)) {
159+
$usingParams.Add($key, $ExecutionContext.SessionState.PSVariable.Get($varPath).Value)
156160
}
157161
}
158162

README.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Enables parallel processing of pipeline input objects.
88

99
```powershell
1010
Invoke-Parallel -InputObject <Object> [-ScriptBlock] <ScriptBlock> [-ThrottleLimit <Int32>]
11-
[-ArgumentList <Hashtable>] [-ThreadOptions <PSThreadOptions>] [-Functions <String[]>] [<CommonParameters>]
11+
[-Variables <Hashtable>] [-Functions <String[]>] [-ThreadOptions <PSThreadOptions>] [<CommonParameters>]
1212
```
1313

1414
## DESCRIPTION
1515

16-
PowerShell function that intends to emulate `ForEach-Object -Parallel` functionality in PowerShell Core for those stuck with Windows PowerShell. This function shares similar usage and capabilities than the ones available in the built-in cmdlet.
16+
PowerShell function that intends to emulate [`ForEach-Object -Parallel`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2#-parallel) for those stuck with Windows PowerShell. This function shares similar usage and capabilities than the ones available in the built-in cmdlet.
1717

18-
This was inspired by RamblingCookieMonster's [`Invoke-Parallel`](https://github.com/RamblingCookieMonster/Invoke-Parallel) and Boe Prox's [`PoshRSJob`](https://github.com/proxb/PoshRSJob) and is merely a simplified take on those.
18+
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) and is merely a simplified take on those with some few improvements.
1919

2020
## REQUIREMENTS
2121

@@ -40,15 +40,15 @@ $message = 'Hello world from {0}'
4040
} -ThrottleLimit 3
4141
```
4242

43-
### EXAMPLE 2: Same as previous example but with `-ArgumentList` parameter
43+
### EXAMPLE 2: Same as previous example but with `-Variables` parameter
4444

4545
```powershell
4646
$message = 'Hello world from {0}'
4747
4848
0..10 | Invoke-Parallel {
4949
$message -f [runspace]::DefaultRunspace.InstanceId
5050
Start-Sleep 3
51-
} -ArgumentList @{ message = $message } -ThrottleLimit 3
51+
} -Variables @{ message = $message } -ThrottleLimit 3
5252
```
5353

5454
### EXAMPLE 3: Adding to a single thread safe instance
@@ -64,7 +64,7 @@ Get-Process | Invoke-Parallel {
6464
$sync
6565
```
6666

67-
### EXAMPLE 4: Same as previous example but using `-ArgumentList` to pass the reference instance to the Runspaces
67+
### EXAMPLE 4: Same as previous example but using `-Variables` to pass the reference instance to the Runspaces
6868

6969
This method is the recommended when passing reference instances to the runspaces, `$using:` may fail in some situations.
7070

@@ -73,7 +73,7 @@ $sync = [hashtable]::Synchronized(@{})
7373
7474
Get-Process | Invoke-Parallel {
7575
$sync[$_.Name] += @( $_ )
76-
} -ArgumentList @{ sync = $sync }
76+
} -Variables @{ sync = $sync }
7777
7878
$sync
7979
```
@@ -91,8 +91,9 @@ function Greet { param($s) "$s hey there!" }
9191
## PARAMETERS
9292

9393
### -InputObject
94+
9495
Specifies the input objects to be processed in the ScriptBlock.
95-
Note: This parameter is intended to be bound from pipeline.
96+
<br>__Note: This parameter is intended to be bound from pipeline.__
9697

9798
```yaml
9899
Type: Object
@@ -107,8 +108,9 @@ Accept wildcard characters: False
107108
```
108109
109110
### -ScriptBlock
111+
110112
Specifies the operation that is performed on each input object.
111-
This script block is run for every object in the pipeline.
113+
<br>This script block is run for every object in the pipeline.
112114
113115
```yaml
114116
Type: ScriptBlock
@@ -123,9 +125,10 @@ Accept wildcard characters: False
123125
```
124126
125127
### -ThrottleLimit
128+
126129
Specifies the number of script blocks that are invoked in parallel.
127-
Input objects are blocked until the running script block count falls below the ThrottleLimit.
128-
The default value is `5`.
130+
<br>Input objects are blocked until the running script block count falls below the ThrottleLimit.
131+
<br>The default value is `5`.
129132

130133
```yaml
131134
Type: Int32
@@ -139,9 +142,10 @@ Accept pipeline input: False
139142
Accept wildcard characters: False
140143
```
141144

142-
### -ArgumentList
143-
Specifies a hashtable of values to be passed to the Runspaces.
144-
Hashtable Keys become the Variable Name inside the ScriptBlock.
145+
### -Variables
146+
147+
Specifies a hash table of variables to have available in the Script Block (Runspaces).
148+
The hash table Keys become the Variable Name inside the Script Block.
145149

146150
```yaml
147151
Type: Hashtable
@@ -155,35 +159,37 @@ Accept pipeline input: False
155159
Accept wildcard characters: False
156160
```
157161

158-
### -ThreadOptions
159-
These options control whether a new thread is created when a command is executed within a Runspace.
160-
See [`PSThreadOptions` Enum](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.psthreadoptions?view=powershellsdk-7.2.0).
161-
Default value is `ReuseThread`.
162+
### -Functions
163+
164+
Existing functions in the Local Session to have available in the Script Block (Runspaces).
162165

163166
```yaml
164-
Type: PSThreadOptions
167+
Type: String[]
165168
Parameter Sets: (All)
166169
Aliases:
167-
Accepted values: Default, UseNewThread, ReuseThread, UseCurrentThread
168170
169171
Required: False
170172
Position: Named
171-
Default value: ReuseThread
173+
Default value: None
172174
Accept pipeline input: False
173175
Accept wildcard characters: False
174176
```
175177

176-
### -Functions
177-
Existing functions in the current scope that we want to have available in the Runspaces.
178+
### -ThreadOptions
179+
180+
These options control whether a new thread is created when a command is executed within a Runspace.
181+
<br>This parameter is limited to `ReuseThread` and `UseNewThread`. Default value is `ReuseThread`.
182+
<br>See [PSThreadOptions Enum](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.psthreadoptions?view=powershellsdk-7.2.0) for details.
178183

179184
```yaml
180-
Type: String[]
185+
Type: PSThreadOptions
181186
Parameter Sets: (All)
182187
Aliases:
188+
Accepted values: Default, UseNewThread, ReuseThread, UseCurrentThread
183189
184190
Required: False
185191
Position: Named
186-
Default value: None
192+
Default value: ReuseThread
187193
Accept pipeline input: False
188194
Accept wildcard characters: False
189-
```
195+
```

tests.ps1 renamed to tests/tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $message = 'Hello world from {0}'
1212
0..10 | Invoke-Parallel {
1313
$message -f [runspace]::DefaultRunspace.InstanceId
1414
Start-Sleep 3
15-
} -ArgumentList @{ message = $message } -ThrottleLimit 3
15+
} -Variables @{ message = $message } -ThrottleLimit 3
1616

1717
# Example 3
1818
$sync = [hashtable]::Synchronized(@{})
@@ -29,7 +29,7 @@ $sync = [hashtable]::Synchronized(@{})
2929

3030
Get-Process | Invoke-Parallel {
3131
$sync[$_.Name] += @( $_ )
32-
} -ArgumentList @{ sync = $sync }
32+
} -Variables @{ sync = $sync }
3333

3434
$sync
3535

0 commit comments

Comments
 (0)