New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$Using variable is not available when run Invoke-Command to different versions of PowerShell. #5722

Open
aetos382 opened this Issue Dec 20, 2017 · 0 comments

Comments

Projects
None yet
2 participants
@aetos382

aetos382 commented Dec 20, 2017

Description

When creating a remote sessions to multiple computers and running Invoke-Command, if different versions of PowerShell are installed on those machines and the session to newer machine is passed first to the -Session parameter, I can't use $Using variable.

I expect to get same result regardless of the order of the arguments.

I think that the error was due to incompatibility between $PSVersionTable.PSRemotingProtocolVersion on these machines.

This problem does not occur in PowerShell 6.0 and 5.1 because these PSRemotingProtocolVersion are same.
For combinations such as 6.0 and 4.0, 5.1 and 4.0, be careful with the order of the -Session arguments.

Steps to reproduce

$cred = Get-Credential

# Windows Server 2012 R2
$older_session = New-PSSession -ComputerName 'computer.running.windows.server.2012.r2' -Credential $cred

# PSRemotingProtocolVersion = 2.2
Invoke-Command -Session $older_session -ScriptBlock { $PSVersionTable }

# Windows Server 2016 (1607)
$newer_session = New-PSSession -ComputerName 'computer.running.windows.server.2016.1607' -Credential $cred

# PSRemotingProtocolVersion = 2.3
Invoke-Command -Session $newer_session -ScriptBlock { $PSVersionTable }

$x = 1

Invoke-Command -Session $newer_session, $older_session -ScriptBlock { $Using:x }

Expected behavior

1
1

Actual behavior

1
A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script wo
rkflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer.
    + CategoryInfo          : InvalidOperation: (:) []、RuntimeException
    + FullyQualifiedErrorId : UsingWithoutInvokeCommand
    + PSComputerName        : computer.running.windows.server.2012.r2

Workaround

Pass the sessions to the -Session parameter in order of PSRemotingProtocolVersion.

Invoke-Command -Session $older_session, $newer_session -ScriptBlock { $Using:x }

Or sort the sessions

$sessions = $sessions | Sort-Object -Property @{
    Expression = { Invoke-Command -Session $_ -ScriptBlock { $PSVersionTable.PSRemotingProtocolVersion } }
    Ascending = $true
}

Invoke-Command -Session $sessions -ScriptBlock { $Using:x }

Environment data

Local machine:

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-rc.2
PSEdition                      Core
GitCommitId                    v6.0.0-rc.2
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Older machine (Windows Server 2012 R2):

Name                           Value                                                                                                    
----                           -----                                                                                                    
PSRemotingProtocolVersion      2.2                                                                                                      
BuildVersion                   6.3.9600.18773                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                     
PSVersion                      4.0                                                                                                      
CLRVersion                     4.0.30319.42000                                                                                          
WSManStackVersion              3.0                                                                                                      
SerializationVersion           1.1.0.1 

Newer machine (Windows Server 2016 1607):

Name                           Value                                                                                                    
----                           -----                                                                                                    
PSRemotingProtocolVersion      2.3                                                                                                      
BuildVersion                   10.0.14393.693                                                                                           
PSVersion                      5.1.14393.693                                                                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                  
PSEdition                      Desktop                                                                                                  
CLRVersion                     4.0.30319.42000                                                                                          
WSManStackVersion              3.0                                                                                                      
SerializationVersion           1.1.0.1   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment