PipelineVariable not available when using Foreach-Object -Parallel
See original GitHub issueSteps to reproduce
$MyDict = [System.Collections.Concurrent.ConcurrentDictionary[string,object]]::new()
Get-Process -PipelineVariable Processinfo | ForEach-Object -Parallel {
$dict = $using:MyDict
$dict.TryAdd($Processinfo.id, $Processinfo) | Out-Null
}
$MyDict.Keys.Count
1
$MyDict.Keys
Expected behavior
$MyDict.Keys.Count
200
$MyDict.Keys
0
2
3
9
1480
...
Actual behavior
$MyDict.Keys.Count
1
$MyDict.Keys
Environment data
$PSVersionTable
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
The variable defined as the pipeline variable is not available in the foreach-object script when the option -Parallel is used, However then -Parallel is not used, the variable can be used correctly
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (7 by maintainers)
Top Results From Across the Web
ForEach-Object - Module: Microsoft.PowerShell.Core
PipelineVariable common parameter variables are not supported in Foreach-Object -Parallel scenarios even with the $using: keyword. Example 17: Passing variables ...
Read more >How can I use the -PipelineVariable parameter with ...
What I am finding, however, is that in my final ForEach-Object block the $_ variable (the PSSession object) doesn't always match up with...
Read more >How to use ForEach-Object Parallel cmdlet in PowerShell
You can see in the above script that only $computers can be accessible inside the Foreach loop via $_ but to access the...
Read more >PowerShell: Practical Use of PipelineVariable Common ...
Practical examples that show how the PipelineVariable parameter can be applied on day-to-day automation and reporting tasks. - PowerShell.
Read more >Should I be using ".ForEach()" and ".Where()" built-in object ...
In several of the PowerShell scripts I have written, I have traditionally used the ForEach cmdlet to iterate through arrays/collections.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pipeline variables are not supported for Foreach-Object -Parallel. The RFC and blog specify that variables can be passed in only via pipeline input
$_
and with the$using:
keyword.But we may need to update the documentation to make this more clear. This may be something that can be considered for future releases.
Thanks for clarifying, @PaulHigin; documenting this will certainly help, but providing an appropriate error message is important too.
If we don’t want to introduce a specific error message for this scenario, the existing, catch-all message should be amended, along the lines of:
Note that I’ve cut out the workflow reference, as it doesn’t apply to PS Core anymore, and that I’ve also added
Start-ThreadJob
.