Invoke-Command in a background job

bones picture bones · Jul 17, 2013 · Viewed 13.7k times · Source

I have a powershell 2.0 script which should run a command on several servers and process the output. I want to run the command and the processing for each server in a background job.

The comand works without any problems and terminates within half a second or less:

Invoke-Command -ComputerName $client -ScriptBlock { #do some stuff }

But when I run this in a background job, the job doesn't terminate:

Start-Job { Invoke-Command -ComputerName $client -ScriptBlock { #do some stuff } }

Has someone a idea what the problem could be?

Answer

Andy Arismendi picture Andy Arismendi · Jul 17, 2013

Looks like you should do it the other way:

http://technet.microsoft.com/en-us/library/hh849698.aspx

To run a background job on a remote computer, use the AsJob parameter that is available on many cmdlets, or use the Invoke-Command cmdlet to run a Start-Job command on the remote computer. For more information, see about_Remote_Jobs.