Skip to content

Variable defined in InitialSessionState is not available in scriptblock originating from another runspace #7593

@alx9r

Description

@alx9r

This came up when experimenting with @lzybkr's workaround for #7530. In short, I'm using the ScriptBlock (not just its string representation) from one runspace in another so that file position information is correctly reported when an error is encountered.

Steps to reproduce

$scriptblock = {
    "a in dot-invoked scriptblock: $a"
}

$powershell = [powershell]::Create().
    AddScript({
        "a in outer scriptblock: $a"
        . $args[0]
    }).
    AddArgument($scriptblock)

$initialSessionState = [initialsessionstate]::CreateDefault()
$initialSessionState.Variables.Add(
    [System.Management.Automation.Runspaces.SessionStateVariableEntry]::new(
        'a',
        1,
        ''
    )    
)
$runspace = [runspacefactory]::CreateRunspace($initialSessionState)
$runspace.Open()

$powershell.Runspace = $runspace
$powershell.Invoke()

Expected behavior

a in outer scriptblock: 1
a in dot-invoked scriptblock: 1

Actual behavior

a in outer scriptblock: 1
a in dot-invoked scriptblock:

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.4
PSEdition                      Core
GitCommitId                    6.1.0-preview.4
OS                             Microsoft Windows 6.3.9600 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions