Skip to content

ForEach-Object -Parallel does not accept a scriptblock variable containing a $using #16445

Closed

Description

@lalku

Prerequisites

Steps to reproduce

After upgrading to PS 7.2 from PS 7.1.5 I am not able to pass a scriptblock variable containing a $using anymore. This works fine in PS 7.1.5

Expected behavior

Using PS 7.1.5

PS>$data = 'hi'
PS>$script = [scriptblock]::Create('Write-Host $using:data')
PS>1..2 | ForEach-Object -Parallel $script
hi
hi

Actual behavior

Using PS 7.2.0

PS>$data = 'hi'
PS>$script = [scriptblock]::Create('Write-Host $using:data')
PS>1..2 | ForEach-Object -Parallel $script
InvalidOperation: 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.
InvalidOperation: 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

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 : 1
            OffsetInLine     : 1
            HistoryId        : -1
            Line             : Write-Host $using:data
            PositionMessage  : At line:1 char:1
                               + Write-Host $using:data
                               + ~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  :
        Name          : GetUsingValue
        DeclaringType : System.Management.Automation.VariableOps, System.Management.Automation, Version=7.2.0.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 : 1
    OffsetInLine     : 1
    HistoryId        : -1
    Line             : Write-Host $using:data
    PositionMessage  : At line:1 char:1
                       + Write-Host $using:data
                       + ~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.19043
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
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Nov 11, 2021
  2. added
    Issue-BugIssue has been identified as a bug in the product
    Needs-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    Issue-BugIssue has been identified as a bug in the product
    on Nov 11, 2021
  3. iSazonov commented on Dec 3, 2021

    @iSazonov
    Collaborator

    Dup #12378

  4. ghost added
    In-PRIndicates that a PR is out for the issue
    on Dec 10, 2021
  5. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Dec 14, 2021
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

    Needs-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.Resolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions