Skip to content

Commit 2550a3a

Browse files
authored
Add check for ScriptBlock wrapped in PSObject to $using used in ForEach-Object -Parallel (PowerShell#17234)
1 parent de04e8d commit 2550a3a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/System.Management.Automation/engine/InternalCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ private void InitParallelParameterSet()
419419
// in which they were created.
420420
foreach (object item in _usingValuesMap.Values)
421421
{
422-
if (item is ScriptBlock)
422+
if (item is ScriptBlock or PSObject { BaseObject: ScriptBlock })
423423
{
424424
ThrowTerminatingError(
425425
new ErrorRecord(

test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' {
55

66
BeforeAll {
77
$sb = { "Hello!" }
8+
$psosb = [psobject]{ "Hello!" }
89
}
910

1011
It "Verifies dollar underbar variable" {
@@ -341,6 +342,11 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' {
341342
{ 1..1 | ForEach-Object -Parallel { $using:sb } } | Should -Throw -ErrorId 'ParallelUsingVariableCannotBeScriptBlock,Microsoft.PowerShell.Commands.ForEachObjectCommand'
342343
}
343344

345+
It 'Verifies error for using script block variable in PSObject' {
346+
347+
{ 1..1 | ForEach-Object -Parallel { $using:psosb } } | Should -Throw -ErrorId 'ParallelUsingVariableCannotBeScriptBlock,Microsoft.PowerShell.Commands.ForEachObjectCommand'
348+
}
349+
344350
It 'Verifies error for script block piped variable' {
345351

346352
$actualError = $sb | ForEach-Object -Parallel { "Hello" } 2>&1

0 commit comments

Comments
 (0)