# Get the return code of remote job.

**URL:** <https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501>\
**Category:** PowerShell Help\
**Created:** [October 18, 2018, 6:51am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501 "2018-10-18T06:51:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramona](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@ramona](https://forums.powershell.org/u/ramona)\
**Post date:** [October 18, 2018, 6:51am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/1 "2018-10-18T06:51:40Z")

</div>

Hi, I am invoking Remote Script with Invoke\_Command. If RemoteScript returns the error code say 2, how can I get this error code on host machine? Below is code snippet.

```
$job = Invoke-Command -ComputerName $SlaveMachine -ScriptBlock {
param([string] $RemoteScript,
[string] $controller
)
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
& powershell.exe -File $RemoteScript -initiator $controller
} -ArgumentList $Script, $thisMachine -Credential $C -AsJob

 

Remote Script:

$Testreturncode = 2

return $Testreturncode
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [October 18, 2018, 7:41am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/2 "2018-10-18T07:41:53Z")

</div>

You’re invoking it with -AsJob, which executes as a background task.

Use Get-Job to retrieve references to background tasks, Wait-Job to stall until completion, and then Receive-Job to retrieve any data that background tasks are holding for you on completion:

```
$Jobs = Get-Job
$Jobs | Wait-Job
$Jobs | Receive-Job
```

---

<div class="post-metadata">

**Author:** ![ramona](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@ramona](https://forums.powershell.org/u/ramona)\
**Post date:** [October 18, 2018, 8:57am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/3 "2018-10-18T08:57:34Z")

</div>

Thanks Joel.

I do get the Job results with receive-job but I am looking specifically for the value of $Testreturncode. I want to capture this return code in a variable and do specific handling based on return code.

&nbsp;

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [October 18, 2018, 9:56am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/4 "2018-10-18T09:56:40Z")

</div>

That depends on what else your function / job does and if it outputs anything prior to the return code as well. On the assumption that the return code is the very last thing that the job outputs, you can reliably do something along these lines:

```
$Job | Wait-Job
$JobOutput = $Job | Receive-Job
$ReturnCode = @($JobOutput)[-1]
```

---

<div class="post-metadata">

**Author:** ![ramona](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@ramona](https://forums.powershell.org/u/ramona)\
**Post date:** [October 18, 2018, 10:20am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/5 "2018-10-18T10:20:18Z")

</div>

Thanks Joel. This works but it seems unreliable. If someone adds even a Write-Output line in scriptBlock after the script, It will not return the expected value in return code.

```
$job = Invoke-Command -ComputerName $SlaveMachine -ScriptBlock {
param([string] $RemoteScript,
[string] $controller
)
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
& powershell.exe -File $RemoteScript -initiator $controller

Write-Output "Test result"
} -ArgumentList $Script, $thisMachine -Credential $C -AsJob

 
```
Wait-Job $job | Out-Null
    $r = Receive-Job $job
    $var = @($r)[-1]
```

Remote Script:

$Testreturncode = 2

return $Testreturncode
```

I do not get the expected output in $var in above case. I tried using $LASTEXITCODE but I dont get the correct value of it outside the scriptblock.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [October 18, 2018, 10:56am UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/6 "2018-10-18T10:56:01Z")

</div>

Return code is not the same as $LASTEXITCODE. The latter is only populated when running a compiled application directly – and you’re running it via a native remoting command.

And yes, additional output may butcher things a bit. **Return** in powershell is not “return just this data”; it’s more analogous to “return this AND all previous output”.

The code I posted ought to work fine, but I think the issue is that you’re calling the powershell.exe instead of just invoking the script in the existing session

If instead you simply invoke the script as-is it should work more easily:

```
#replace: & powershell.exe -File $RemoteScript -initiator $controller
& $RemoteScript -Initiator $Controller
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/get-the-return-code-of-remote-job/11501/7 "2024-05-16T20:34:14Z")

</div>


