Skip to content

In remoting calls, combining the $using: scope with namespace variable notation is quietly ignored #16019

Closed

Description

Prerequisites

Steps to reproduce

Possibly related: #9204

Using namespace variable notation in combination with the $using: scope works as expected with jobs; e.g.:

$env:FOO = 'bar'
# Outputs 'bar', as expected.
Start-Job { $using:env:FOO } | Receive-Job -Wait -AutoRemoveJob

By contrast, in the context of remoting it fails quietly (the reference evaluates to $null):

# Run from an elevated session on a machine with remoting enabled.
$env:FOO = 'bar'
Invoke-Command -ComputerName . { $using:env:FOO }

Expected behavior

'bar' should print.

Actual behavior

Nothing is output.

Error details

No response

Environment data

PowerShell Core 7.2.0-preview.9

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Aug 30, 2021
  2. added
    WG-Remoting
    on Aug 30, 2021
  3. dwtaber commented on Sep 10, 2021

    @dwtaber
    Contributor

    Huh, this is interesting. When using the -Session parameter rather than -ComputerName, Invoke-Command {$using:env:foo} does return the expected value.

    Edit: same for $using:function:foo, $using:global:foo, and $using:variable:foo.

  4. dwtaber commented on Sep 13, 2021

    @dwtaber
    Contributor

    I've identified the proximate cause of this issue, but not yet the root cause. If you're using the -ComputerName parameter, because the remote runspace isn't opened yet, the PowerShell version on the remote computer is assumed to be PowerShell v2:

    /// <summary>
    /// Check the powershell version of the remote server.
    /// </summary>
    private static string GetRemoteServerPsVersion(RemoteRunspace remoteRunspace)
    {
    if (remoteRunspace.ConnectionInfo is NewProcessConnectionInfo)
    {
    // This is for Start-Job. The remote end is actually a child local powershell process, so it must be PSv5 or later
    return PSv5OrLater;
    }
    PSPrimitiveDictionary psApplicationPrivateData = remoteRunspace.GetApplicationPrivateData();
    if (psApplicationPrivateData == null)
    {
    // The remote runspace is not opened yet, or it's disconnected before the private data is retrieved.
    // In this case we cannot validate if the remote server is running PSv5 or later, so for safety purpose,
    // we will handle the $using expressions as if the remote server is PSv2.
    return PSv2;
    }
    // Unfortunately, the PSVersion value in the private data from PSv3 and PSv4 server is always 2.0.
    // This got fixed in PSv5, so a PSv5 server will return 5.0. That means we need other way to tell
    // if the remote server is PSv2 or PSv3+. After PSv3, remote runspace supports connect/disconnect,
    // so we can use it to differentiate PSv2 from PSv3+.
    if (remoteRunspace.CanDisconnect)
    {
    Version serverPsVersion = null;
    PSPrimitiveDictionary.TryPathGet(
    psApplicationPrivateData,
    out serverPsVersion,
    PSVersionInfo.PSVersionTableName,
    PSVersionInfo.PSVersionName);
    if (serverPsVersion != null)
    {
    return serverPsVersion.Major >= 5 ? PSv5OrLater : PSv3Orv4;
    }
    // The private data is available but we failed to get the server powershell version.
    // This should never happen, but in case it happens, handle the $using expressions
    // as if the remote server is PSv2.
    Dbg.Assert(false, "Application private data is available but we failed to get the server powershell version. This should never happen.");
    }
    return PSv2;
    }

    Because PowerShell v2 doesn't understand $Using: expressions, GetPowerShellForPSv2() is supposed to alter the script before sending to the remote computer. I'd guess that whatever parsing happens in GetConvertedScript doesn't properly account for cases like $using:env:foo, but I haven't had a chance to dig in yet.

    To test whether I was on the right track, I modified CreatePipeline, replacing the conditional expression on lines 1882–1884 with GetPowerShellForPSv3OrLater(PSv5OrLater) to ignore version checking and assume the remote computer would use PowerShell v5 or later. Sure enough, Invoke-Command started returning values for $using:env:foo, $env:variable:foo, etc. Obviously it's not acceptable for production to just do away with version checking, but it at least helps pin down the cause. I'll keep digging.

  5. dwtaber commented on Sep 13, 2021

    @dwtaber
    Contributor

    Looks like it's not just an issue with namespace variable notation. The following also returns null:

    $Foo = [datetime]
    Invoke-Command -ComputerName $ComputerName {$using:Foo::Now}
  6. added a commit that references this issue on Sep 16, 2021
    c5c6882
  7. added
    Issue-Bug
    and removed
    Needs-Triage
    on Sep 17, 2021
  8. PaulHigin commented on Sep 17, 2021

    @PaulHigin
    Contributor

    WG-Remoting

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

    Issue-BugResolution-FixedWG-Remoting

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions