Play the first section of PowerShell-"background task Processing"-Technology & Sharing

Source: Internet
Author: User

Overview

I believe you are not familiar with the background tasks, such as. NET background threading, Java threading, and so on.

And how do we start a background task when we use the powerful tools of PowerShell, and how to handle these tasks, this will tell you about PowerShell background task processing.

Directory
    • 1. When will the background task be used?
    • 2. What are the main scenarios for background tasks
    • 3.PowerShell inside how to open background task
      • Start-job
      • Invoke-command-asjob
    • 4. Processing Job
      • Wait-job
      • Remove-job
    • 5.start-job Pass
    • 6. References & extended Reading
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. What are the main scenarios for background tasks

(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

Wait a minute

3.PowerShell inside how to open background task(1) Start-job
1 $StartJob = start-job-scriptblock {get-Process}2$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
 1   $InvokeCommandJob  = Invoke-command-computername Localhost-scriptblock {get-process }-asjob  2   $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
 1  #   2   $WaitJob 1  = Start-job-scriptblock {sleep-seconds 10 3  #   Create a background task that waits for 5s  4   $WaitJob 2  = start-job-scriptblock {sleep-seconds 15}  5  #   6  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 The status of $waitjob2 can be seen from the above execution results For running, because the wait time is not long enough, if the waiting time-out is changed a bit longer, the code is as follows:
1 $WaitJob 1,$WaitJob 2 -timeout 10

There are also some commands waiting for the job to complete

1 $WaitJob 1. Id,$WaitJob 2. Id-timeout2$WaitJob 1. Name,$WaitJob 2. Name-timeout3$WaitJob 1. InstanceId,$WaitJob 2. Instanceid-timeout4 wait-job-state running-timeout 10
From the example above we can monitor the status of the Job by setting timeout time (2) remove-job
1 $WaitJob 1. Id,$WaitJob 2. Id2$WaitJob 1. Name,$WaitJob 2. Name3$WaitJob 1. InstanceId,$WaitJob 2. InstanceId4 remove-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
1 $WaitJob 1,$WaitJob 2 -force

Here's a very interesting question for me.

5.start-job PassFor example, we are now going to open a job, calculate $c= $a + $b, and then write the results to the D:\a.txt text file
1 $a = 1  2$b = 2  3$c = 0  4$Job = start-job- Scriptblock {  5         $c$a$b   6         $c " D:\a.txt "  7     }
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:
1 $a= 12 $b= 23 $c= 04 $Job= Start-job-argumentlist$a,$b-Scriptblock {5         param($a,$b)6         $c=$a+$b7         $c>"D:\a.txt"8}

Running the above code, you can see that the content in the text file is 3.

6. References & extended reading

The following links provide technical information for Microsoft:

http://technet.microsoft.com/library/2bc04935-0deb-4ec0-b856-d7290cca6442 (v=wps.630). aspx

Play the first section of PowerShell-"background task Processing"-Technology & Sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.