question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSSession return local environment variable, not remote variable

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
SteveL-MSFTcommented, Jan 13, 2021

@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.

3reactions
PaulHigincommented, Jan 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found