Powershell Background Jobs One of the great features of the powershell is background jobs, the term itself describes as whatever the script code you execute as a job runs in the background, the powershell commands that take longer time to execute or accomplish a specific task on the machine or remote machines can be run in the background, while the background job is running you can still use the powershell window for synchronous commands, and the background jobs will be suppressed until the synchronous command finishes its execution.
Each powershell job runs as temporary connection and it will be removed when the job gets completed. To start executing the background jobs you can use the start-job command or Asjob, let’s check the examples.
stat-job -scriptblock {get-service}
The above command runs in the background and gets all the services available on the local machine, and to see the status of the job then there is a command get-job <job name> get-job job1, this command brings state of the job as complete, failed or running. When the job is completed, you can see the results using the receive-job command, let’s see the examples.
receive-job job1
This command brings all the information of background job on the screen, in our case it lists all the services available on the local machine.
When you are using the invoke-command running and running the background jobs using the parameter -Asjob there will be many child jobs, each child job gets a name, ID, and instance ID. To see the parent and child jobs of a job you should use the parameter IncludeChildJobs of get-job command. Let’s see some examples.
get-job job3 -IncludeChildjob
This job brings the status of the parent job and all its child jobs.
#backgrounds jobs! Invoke-command -Computername “server1″,”server2” -ScriptBlock {get-service} -Asjob
This job runs in the background and has child job as well, each child job runs independently and completes its job, in our case it gets all the services available on the remote servers server1 and server2, to see those jobs information we use receive-job jobID.
Let’s restart a service on remote machines, if you have a requirement to restart a service on many servers then we can write the script to run in the background as follows.
#storing the servers list in a variable, you have the servers list in a text file. #server list in c:\temp\allservers.txt $servers= get-content -path "C:\temp\allservers.txt" foreach ($server in $Servers) Invoke-command -ComputerName $server -ScriptBlock (restart-service -name MSP} -Asjob #if you want to store the job in a variable then #myjob = Invoke-command -ComputerName $server -ScriptBlock (restart-service -name MSP} -Asjob }
The above script runs in the background as a job and restart the service MSP on the remote machines that you mentioned the text file allservers.txt
Lastly, if you want to delete the background job, when you don’t want to run then you can use the command remove-job.
Example:
Remove-Job Job10
if you want to remove all the jobs which are running in the background then simply use the command as follows.
get-job | remove-job -Force
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply