How to capture the Return Value of a ScriptBlock invoked with Powershell's Invoke-Command?

$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 Create a new session using new-pssession Invoke your scripblock in this session Fetch the lastexitcode from this session.

This worked. I didn't know that you could pass remote variables from a session back to the local script like that. Thanks!

– Jay Spang 2 days ago 1 Wouldn't $remotelastexitcode = invoke-command -ScriptBlock { cmd /c exit 2; $lastexitcode} -Session $remotesession work? Since you are using the sessions to give multiple commands, you can probably prevent that. – manojlds yesterday @manojlds yes, capturing the lastexitcode in the first scriptblock will also work.

– jon Z 17 hours ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions