1. When will the background task be used?
When the local machine needs to perform an action, this action takes a long time, and the machine can only wait during that time, and we want to be able to do other things during the time the machine is waiting.
This way we can use the background task to solve the problems encountered above.
2. Background tasks are mainly used in which scenarios, for example
(1) Copy files to multiple remote machines, can open multiple tasks for parallel copy
(2) Multiple remote machine installation files simultaneously, can open multiple tasks for parallel installation
3.PowerShell inside how to open background tasks (1)
Start-job
# $StartJob = start-job-scriptblock {get-process}# $StartJob
Id Name psjobtypename State hasmoredata Location Command
-- ---- ------------- ----- ----------- -------- -------
7 Job7 backgroundjob Running True localhost get-process
We can see from above that the type of $startjob called Job7,job is a background job, and the status of Running,command represents the execution of the command get-process (2)Invoke-command-asjob
# $InvokeCommandJob = Invoke-command-computername localhost-scriptblock {get-process}-asjob# $InvokeCommandJob
Id Name psjobtypename State hasmoredata location Command
-- ---- ------------- ----- ----------- -------- -------
Job15 remotejob Running True LocalHost get-process
We can see from above the type of $invokecommandjob is the background remotejob, indicating the remote execution of the job, here I use the remote machine for localhost local machine as a test machine, you can use the remote machine name to replace the local localhost
After we've started a task, we can't ignore it, and now we're going to deal with the job.
4. Processing job (1)Wait-job
#创建一个等待1s的后台任务 $WaitJob 1 = start-job-scriptblock {sleep-seconds 1} #创建一个等待5s的后台任务 $WaitJob 2 = start-job- Scriptblock {sleep-seconds 5} #等待两个Job 2s completed wait-job-job $WaitJob 1, $WaitJob 2-timeout 2
Id Name psjobtypename State hasmoredata Location Command
-- ---- ------------- ----- ----------- -------- -------
Job31 Backgroundjob completed False localhost sleep-seconds 1
Job33 backgroundjob Running True localhost sleep-seconds 5 from the above execution results can be seen $wa The status of ITJOB2 is running, because the wait time is not long enough, if the waiting time-out is changed a little longer, the code is as follows:
Wait-job-job $WaitJob 1, $WaitJob 2-timeout 10
There are also some commands waiting for the job to complete
Wait-job-id $WaitJob 1.Id, $WaitJob 2.id-timeout 10wait-job-name $WaitJob 1.Name, $WaitJob 2.name-timeout 10wait-job- InstanceId $WaitJob 1.InstanceId, $WaitJob 2.instanceid-timeout 10wait-job-state running-timeout 10
With the above example we can monitor the status of the job by setting timeout time (2)Remove-job
Remove-job-job $WaitJob 1, $WaitJob 2remove-job-id $WaitJob 1.Id, $WaitJob 2.idremove-job-name $WaitJob 1.name,$ Waitjob2.nameremove-job-instanceid $WaitJob 1.InstanceId, $WaitJob 2.instanceidremove-job-state completed
When you Remove the job, be aware that if the job is not completed or is stopped, an exception is thrown:
Remove-job:the command cannot remove the job with the job ID because the job was not finished. To remove the job, first
Stop the job, or use the force parameter.
Parameter name:job
If you want to force the remove Job, you can add a parameter later-force
Remove-job-job $WaitJob 1, $WaitJob 2-force
Here's a very interesting question for me.
5.start-job For example we are now going to open a job, calculate $c= $a + $b, and then write the results to a D:\a.txt text file
$a = 1$b = 2$c = 0$job = start-job-scriptblock {$c = $a + $b $c > "D:\a.txt"}
OK, the code is written, after the run, we expect to get the result is the content of the D:\a.txt text file is 3 but in fact the content in the text file is empty, this is why? Because we have overlooked a problem: The parameters inside the Scriptblock are required to be passed in. Modify the above code:
$a = 1$b = 2$c = 0$job = Start-job-argumentlist $a, $b-scriptblock {param ($a, $b) $c = $a + $b $c > "D:\a.txt"}
Running the above code, you can see that the content in the text file is 3
Play the first section of the PowerShell is written here today, thank you Bo friends Read this article, the article content there are many areas need to improve, there will be more sharing. Bo friends have any questions, welcome message, I will be in time to communicate with you.
Play the first section of PowerShell-background task processing-Technology & sharing