Skip to content

Unable to use using keyword in a ScriptBlock instantiated via [ScriptBlock]::create in PowerShell 7.2.0 with ForEach-Object -Parallel #16551

@danielealbano

Description

@danielealbano

Prerequisites

Steps to reproduce

On PowerShell 7.2.0 execute the following code

$myVariable = "I am a variable"
$sb = [scriptblock]::Create('Write-Host "MyVariable: $using:MyVariable"')
1..1 | foreach -Parallel $sb

Expected behavior

Should print out

MyVariable: I am a variable

This is the behaviour with PowerShell 7.1.5

Actual behavior

In throws the following error

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

> 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 : 1
            OffsetInLine     : 1
            HistoryId        : -1
            Line             : Write-Host "MyVariable: $using:MyVariable"
            PositionMessage  : At line:1 char:1
                               + Write-Host "MyVariable: $using:MyVariable"
                               + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            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 "MyVariable: $using:MyVariable"
    PositionMessage  : At line:1 char:1
                       + Write-Host "MyVariable: $using:MyVariable"
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

> $PSVersionTable

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

image

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-DuplicateThe issue is a duplicate.WG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions