Skip to content

Foreach-Object -Parallel [scriptblock]::Create("$Using:SomeVariable") Does not work on pwsh 7.2.* #17571

Closed

Description

@Bleep5143

Prerequisites

Steps to reproduce

Below are two snippets demonstrating seemingly identical behavior, however the bottom snippet does not work.

# This Works
$VariableOutsideParallel = 'something'
1..1 | ForEach-Object -Parallel {
  Write-Host $Using:VariableOutsideParallel
}
# This does NOT on 7.2.0+ (But does on 7.1.*)
$VariableOutsideParallel = 'something'
[scriptblock]$SB = {
  Write-Host $Using:VariableOutsideParallel
}
1..1 | ForEach-Object -Parallel $SB

Expected behavior

PS> $VariableOutsideParallel = 'something'
PS> [scriptblock]$SB = {
  Write-Host $Using:VariableOutsideParallel
}
PS> 1..1 | ForEach-Object -Parallel $SB
something

Actual behavior

PS> $VariableOutsideParallel = 'something'
PS> [scriptblock]$SB = {
  Write-Host $Using:VariableOutsideParallel
}
PS> 1..1 | ForEach-Object -Parallel $SB
InvalidOperation:
Line |
   2 |    Write-Host $Using:VariableOutsideParallel
     |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer.

Error details

PS> Get-Error

Exception             :
    Type        : System.Management.Automation.RuntimeException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command,
Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid
only if the script block is invoked on a remote computer.
            HResult : -2146233087
        CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : UsingWithoutInvokeCommand
        InvocationInfo        :
            ScriptLineNumber : 2
            OffsetInLine     : 3
            HistoryId        : -1
            Line             :   Write-Host $Using:VariableOutsideParallel

            PositionMessage  : At line:2 char:3
                               +   Write-Host $Using:VariableOutsideParallel
                               +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 2
    TargetSite  :
        Name          : GetUsingValue
        DeclaringType : System.Management.Automation.VariableOps, System.Management.Automation, Version=7.2.4.500,
Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command,
Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid
only if the script block is invoked on a remote computer.
    Data        : System.Collections.ListDictionaryInternal
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  :
   at System.Management.Automation.VariableOps.GetUsingValue(MutableTuple tuple, String usingExpressionKey, Int32
index, ExecutionContext context)
   at System.Management.Automation.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : UsingWithoutInvokeCommand
InvocationInfo        :
    ScriptLineNumber : 2
    OffsetInLine     : 3
    HistoryId        : -1
    Line             :   Write-Host $Using:VariableOutsideParallel

    PositionMessage  : At line:2 char:3
                       +   Write-Host $Using:VariableOutsideParallel
                       +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 2

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.5
PSEdition                      Core
GitCommitId                    7.2.5
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Jun 23, 2022
  2. Bleep5143 commented on Jun 23, 2022

    @Bleep5143
    Author

    To add to this, I just installed 7.3.0-preview5 and was able to run the snippet successfully.

    PowerShell 7.3.0-preview.5
    PS> $VariableOutsideParallel = 'something'
    >> [scriptblock]$SB = {
    >>   Write-Host $Using:VariableOutsideParallel
    >> }
    >> 1..1 | ForEach-Object -Parallel $SB
    something
    PS> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      7.3.0-preview.5
    PSEdition                      Core
    GitCommitId                    7.3.0-preview.5
    OS                             Microsoft Windows 10.0.19044
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
  3. Northman-de commented on Jun 23, 2022

    @Northman-de

    Please take a look at #16445

  4. jborean93 commented on Jun 23, 2022

    @jborean93
    Collaborator

    cc Paul Higinbotham (@PaulHigin) another reason why the fix should be backported to 7.2.x.

  5. PaulHigin commented on Jun 23, 2022

    @PaulHigin
    Contributor

    Yeah, I was concerned about regressions in our stable branch, but if this is affecting users we should reconsider back porting it. I'll submit it for backport again.

  6. added
    Resolution-Answered
    and removed
    Needs-Triage
    on Jun 23, 2022
  7. Bleep5143 commented on Jun 23, 2022

    @Bleep5143
    Author

    Looks like I didn't search the existing issues well enough... sorry about that. Thank you for the guidance!

  8. ghost closed this as completedon Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions