You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In remoting calls, combining the $using: scope with namespace variable notation is quietly ignored #16019
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:
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.
Reacted by Michael Klement and Rain Sallow (/u/ta11ow)
Activity
dwtaber commentedon Sep 10, 2021
Huh, this is interesting. When using the
-Sessionparameter 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.dwtaber commentedon Sep 13, 2021
I've identified the proximate cause of this issue, but not yet the root cause. If you're using the
-ComputerNameparameter, because the remote runspace isn't opened yet, the PowerShell version on the remote computer is assumed to be PowerShell v2:PowerShell/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs
Lines 1900 to 1945 in dad7a34
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 inGetConvertedScriptdoesn'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 withGetPowerShellForPSv3OrLater(PSv5OrLater)to ignore version checking and assume the remote computer would use PowerShell v5 or later. Sure enough,Invoke-Commandstarted 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.dwtaber commentedon Sep 13, 2021
Looks like it's not just an issue with namespace variable notation. The following also returns null:
Fixes PowerShell#9204. Fixes PowerShell#16019.
PaulHigin commentedon Sep 17, 2021
WG-Remoting