Powershell : Receive-Job : The background process reported an error with the following message: .

# Powershell : Receive-Job : The background process reported an error with the following message: .

• Monday, October 29, 2012 5:00 PM

I am trying to use Start-Job to simultaneously execute multiple commands remotely using PsExec, but I am running into a problem sometimes, I narrowed down the problem to the small example below:

$job = start-job -ScriptBlock { Start-Process C:\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s hostname" -wait -NoNewWindow -PassThru } So, what this shoud do is create a job, the job is to create a process using PsExec. PsExec in this example will remotely execute the "hostname" command on "MYREMOTEPC" I try to get the results of the Job using: Receive-Job$job

And I get the following Error:

Receive-Job : The background process reported an error with the following message: .
At line:1 char:12
+ CategoryInfo          : OperationStopped: (System.Manageme...emotingChildJob:PSRemotingChildJob) [Receive-Job], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure,Microsoft.PowerShell.Commands.ReceiveJobCommand

When I run the start-process command outside of the job it works:

Start-Process C:\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s hostname" -wait -NoNewWindow -PassThru
Output:
PsExec v1.98 - Execute processes remotely
Sysinternals - www.sysinternals.com

Connecting to MYREMOTEPC...
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
74      10     1964       5260    66     0.00   2712 PsExec

MYREMOTEPC
hostname exited on MYREMOTEPC with error code 0.

• Edited by Monday, October 29, 2012 5:03 PM Added details about running the command outside of Start-Job
• Edited by Monday, October 29, 2012 5:04 PM
•

### All Replies

• Monday, October 29, 2012 5:41 PM

Why would you use PsExec in a network with POwerSHell.  All of that info is available remotely via PosH and WMI and it is much easier and faster to use.

Carefully read the help for 'Start-Job'.

You would not need wait and passthru with Start-Job and it would not work anyway.

PsExec runs a command on a remote server and retruns any text output.  It cannot return active objects.

Start-Process with -Passthru is returning the Process object.  This is an active object not usable from a job.

In most cases you cannot use PsExec with a PowerShell Job.

¯\_(ツ)_/¯

• Monday, October 29, 2012 6:15 PM

The PassThru DOES work, as does the Wait, so I am not sure what you mean that I would not need them and they would not work...

$p = Start-Process C:\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s hostname" -wait -NoNewWindow -PassThru$p.ExitCode

The above code outputs "0" because that is the ExitCode that PsExec returned. If i change "hostname" to "hoostname" in the above command, it outputs an ExitCode of 2. So PassThru IS working and working as expected in the above example. I used the "hostname" command just as an example, I am not actually trying to retreive the hostname command this way, instead, I will be executing a .exe file remotely and I need to get the exitCode to find out if it ran successfully .My example above shows that I can do that.

I need the -wait because in my final script, i will have other logic in the ScriptBlock that needs to wait until the PsExec process is finished. And in my example above, it functions as expected and it waits until PsExec is done, then it moves on to the $p.ExitCode line to output the Exit Code. What I do not understand is why this is failing when it is nested in a Start-Job command, when I run this command by itself everything runs as expected... • Monday, October 29, 2012 8:00 PM If they work then why are you posting? YOu cannot return an active object to a job. You can to a commandline. Why not use a better example with a program that returns an exitcode. WEHy do you think you n3ed passthru with a job. The exit code will always be returned if it exists for the process you have started. PsExec does not work well with PowerShell jobs. Start process is not reqiuied to ruin a remote wiuth PsExec. Here is a working example: 15:58 PS>Start-Job -script {e:\test2\pstools\PsExec \\omega2 -s hostname} Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 3 Job3 Running True localhost e:\test2\pstools... 15:59 PS>get-job job4 |select -exp error + CategoryInfo : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError PsExec v1.98 - Execute processes remotely Copyright (C) 2001-2010 Mark Russinovich Sysinternals - www.sysinternals.com Connecting to local system...Starting PsExec service on local system...Connecting with PsExec servi ce on omega2...Starting hostname on omega2... hostname exited on omega2 with error code 0. 15:59 PS> ¯\_(ツ)_/¯ • Monday, October 29, 2012 8:08 PM The following shows how PSexec throws an exceptiopn when run as a job: 16:07 PS>$job.ChildJobs[0].Error

+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Sysinternals - www.sysinternals.com

c:\windows\system32\cmd.exe exited with error code 0.

¯\_(ツ)_/¯

• Monday, October 29, 2012 9:08 PM

This is why I think I need passthru, because this works and I have the exit code stored in a variable and can use it, return it, or do whatever I want with it:

$p = Start-Process C:\Users\u1000\Documents\backup_script\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s \\MYSERVER\myshare\snapshot.bat c: \\MYSERVER\myshare\MYREMOTEPC.img" -wait -NoNewWindow -PassThru$p.ExitCode

And this Example does not give me the Exit Code. Maybe there is another way to store the exit code in this example that I do not know?

$p = Start-Process C:\Users\u1000\Documents\backup_script\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s \\MYSERVER\myshare\snapshot.bat c: \\MYSERVER\myshare\MYREMOTEPC.img" -wait -NoNewWindow$p.ExitCode

I do understand what you are saying about not being able to pass an object back to a job, so my example was bad... But, shouldn't you be able to pass a text string back to a job as a result?

For example, the following works:

$job = Start-Job -ScriptBlock {$myresult = 123
$myresult } Receive-Job$job

The above receive job command outputs 123, as expected, my job passed 123 back to the job as text...

So, shouldn't I be able to do the same thing, like this:

$job = Start-Job -FilePath .\Backup_Test.ps1 .\Backup_Test.ps1: $p = Start-Process C:\PsTools\PsExec.exe -ArgumentList "\\MYREMOTEPC -s \\MYSERVER\myshare\snapshot.bat c: \\MYSERVER\myshare\MYREMOTEPC.img" -wait -NoNewWindow -PassThru
$p.ExitCode So, my Backup_Test.ps1 isn't returning the process object anymore, instead, it is returning$p.ExitCode, which isn't an object, it is a simple value.

Yet, in the above example, I still get the error message when I try to receive the job.

You say PsExec doesn't work well with PowerShell, and that I don't need to use Start-Process, but I'm not understanding why. PsExec is just an executable, and PowerShell is fully capable of running an executable, isn't it? I chose Start-Process as my method, because If I don't use Start-Process, i don't think I can capture the ExitCode... (http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx This is the article that lead me to Choose Start-Process, it seems to give you the most control over the process, and allows you to capture the exit code).

• Monday, October 29, 2012 9:25 PM

Sorry but that won't work as you have already seen.

¯\_(ツ)_/¯

• Wednesday, October 31, 2012 12:27 PM

I realize that isn't working, that is why I'm here. What I am asking is: Why doesn't it work, and if there is nothing I can do to get it to work, what can I do to achieve the same end result?

Why does PsExec throw an error when run as a job? How does PsExec even know it is being run from within a job, what is different about the environment that it is run within that could possibly make a difference to an external application like PsExec? Is there another way I can run multiple PsExec command simultaniously, while still capturing the Exit Code?

If not PsExec, is there a way I can remotely execute commands as the system account without PsExec?

• Monday, December 31, 2012 3:44 PM
Moderator

Hi,

Do you still need help with this question?

Bill

• Wednesday, January 23, 2013 4:49 PM
Moderator