title: How to capture the Return Value of a ScriptBlock invoked with Powershell's Invoke-Command
body: <p>My question is very similar to <a href="https://stackoverflow.com/questions/1210942/catching-return-code-of-a-command-with-invoke-command-powershell-2">this one</a>, 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 my code:</p>
  
  <pre><code>Invoke-Command -computername $server {\\fileserver\script.cmd $args} -ArgumentList $args
  exit $LASTEXITCODE
  </code></pre>
  
  <p>The problem is that Invoke-Command doesn't capture the return code of script.cmd, so I have no way of knowing if it failed or not. I need to be able to know if script.cmd failed.</p>
  
  <p>I tried using a New-PSSession as well (which lets me see script.cmd's return code on the remote server) but I can't find any way to pass it back to my calling Powershell script to actually DO anything about the failure.</p>