PSSession return local environment variable, not remote variable
See original GitHub issueI have two server, server1 and server2.
I invoke command on server1 and want to get machine environment on server2 by remoting.
ex: [Environment]::GetEnvironmentVariable("path", "machine")
but return the environment variable on server1.
I changed GetEnvironmentVariable
to MachineName
and had the same problem.
Steps to reproduce
PS C:\Users\abc>Enter-PSSession server2
[server2]: PS C:\Users\abc\Documents> [Environment]::MachineName
[server2]: PS C:\Users\abc\Documents> Write-Output ([Environment]::MachineName)
[server2]: PS C:\Users\abc\Documents> $env:COMPUTERNAME
[server2]: PS C:\Users\abc\Documents> Write-Output ($env:COMPUTERNAME)
[server2]: PS C:\Users\abc\Documents> Write-Output ([Environment]::MachineName + "," + $env:COMPUTERNAME)
Expected behavior
server2
server2
server2
server2
server2,server2
Actual behavior
server2
server1 # <-- diff here, Write-Host ([Environment]::MachineName)
server2
server2
server2,server2
Environment data
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:15 (7 by maintainers)
Top Results From Across the Web
about Remote Variables - PowerShell
Explains how to use local and remote variables in remote commands.
Read more >How to return a variable from a remote PowerShell session ...
Just return the variable from the session's scriptblock, either with Write-Output or return or just use the variable in another command as ...
Read more >Using a local variable in a remote PowerShell session
To overcome this gap, PowerShell provides three methods to pass local variables to remote sessions. You can use them with Invoke-Command and New ......
Read more >How to handle local & remote variables using Invoke-Command
PowerShell takes the variable $MyVariable and passes it into the remote session as $LocalVariable. You can change the names of these variables ......
Read more >Powershell : Get variables from remote machine and store ...
Capturing Data from a remote session: New-PSSession -ComputerName localhost -Credential (Get-Credential) | Out-Null $a = Invoke-Command ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@PowerShell/powershell-committee has reviewed this, in general we stand by our original decision to change the behavior so that expressions are evaluated on the remote side as users would expect. However, we are concerned about JEA scenarios with no language mode which will no longer work. We’ll be reaching out to some internal partners that rely on JEA to get more data.
Yes, this client side evaluation was implemented on purpose. But I wasn’t part of the decision at the time and can only speculate as to why this was done. So I think it makes sense to let the committee review and confirm this is the behavior we want.