My question is very similar to this one, except I\'m trying to capture the return code of a ScriptBlock using Invoke-Command (so I can\'t use the -FilePath option). Here\'s
$remotesession = new-pssession -computername localhost
invoke-command -ScriptBlock { cmd /c exit 2} -Session $remotesession
$remotelastexitcode = invoke-command -ScriptBlock { $lastexitcode} -Session $remotesession
$remotelastexitcode # will return 2 in this example