I am confused about the right way to get a return code from a scriptblock called by invoke-command.
My code looks like:
$my_session = new-pssession -computername $my_computer -credential $my_credentials
$my_exit = invoke-Command -Session $my_session -ScriptBlock {
...
...
if ($freespace_mb -lt 10000)
{
write-host "Not enpogh disk space"
$returncode = 2
$returncode
exit
}
...
$a = 10
$b = 100
$c = 1000
...
...
$returncode = "everything is ok"
$returncode
}
write-host $my_exit
I would expect that the content of $my_exit would be either 2 or “everything is ok” based on the results of the test about freespace.
I find instead the expected contents plus garbage, maybe coming from the contents of other variables.
What is the right syntax to get what I expect and only what I expect?
Regards
marius