I am trying to run a piece of remote code using powershell remoting and getting some strange behavior which I am unable to explain. This is the sequence of commands I run.
$sb1 = {$r1 = 1; $r2 = 2; $r3 = Get-Culture; return $r3}
$sb2 = {1; 2; $r3 = Get-Culture; return $r3}
$session = New-PSSession -ComputerName $comp -Credential $creds
$ret1 = Invoke-Command -Session $Session -ScriptBlock $sb1
$ret2 = Invoke-Command -Session $Session -ScriptBlock $sb2
$ret1
>>> en-US
$ret2
>>> 1
Does anyone know a reason for this behavior? I find it very odd. The return statement is ignored, and the scriptblock is evaluated to the first 'uncaptured' expression. Hmmm?
Also, if I did want this block to always evaluate to the return statement, or even the last statement, does anyone know how I might accomplish that?