PowerShell-background jobs, asynchronous operation instances

Source: Internet
Author: User

If you need to speed up the execution of a script, you may need to use a background job. Background jobs can run multiple events in the script at the same time

PowerShell is a single-threaded session and can only do one thing at a time. Background jobs can add additional PowerShell processes to the background processing job. It solves the problem well when the program needs to run concurrently and the amount of data is not very large. But returning data from the PowerShell backend is a cumbersome task that will waste a lot of time. Will cause the script to be slower.

1. There are 3 concurrent execution tasks:

The code is as follows:


$start = Get-date

# Get all Hotfixes
$task 1 = {Get-hotfix}

# Get all scripts in your profile
$task 2 = {Get-service | Where-object Status-eq Running}

# Parse log file
$task 3 = {Get-content-path $env: Windir\windowsupdate.log | Where-object {$_-like ' *successfully installed* '}}

# run 2 tasks in the background, and 1 in the foreground task
$job 1 = start-job-scriptblock $task 1
$job 2 = Start-job-scriptblock $task 2
$result 3 = Invoke-command-scriptblock $task 3

# Wait for the remaining tasks to complete (if not done yet)
$null = wait-job-job $job 1, $job 2

# Now they is done, get the results
$result 1 = receive-job-job $job 1
$result 2 = receive-job-job $job 2

# Discard the jobs
Remove-job-job $job 1, $job 2

$end = Get-date
Write-host-foregroundcolor Red ($end-$start). TotalSeconds

It takes 5.9 seconds to perform all the tasks above. The results of the three missions will be credited to $result1, $result 2, and $result 3 respectively.


2. Let's continue to see how long it will take to complete the command in the foreground:

The code is as follows:


$start = Get-date

# Get all Hotfixes
$task 1 = {Get-hotfix}

# Get all scripts in your profile
$task 2 = {Get-service | Where-object Status-eq Running}

# Parse log file
$task 3 = {Get-content-path $env: Windir\windowsupdate.log | Where-object {$_-like ' *successfully installed* '}}

# run them all in the foreground:
$result 1 = invoke-command-scriptblock $task 1
$result 2 = Invoke-command-scriptblock $task 2
$result 3 = Invoke-command-scriptblock $task 3

$end = Get-date
Write-host-foregroundcolor Red ($end-$start). TotalSeconds

As a result, this time it took only 5.05 seconds. It is almost complete at the same time as background jobs, so background jobs are better suited for long-run tasks. The benefit of the data returned from three tasks is that the number of hits in the foreground can reduce the overhead of the execution process.

Reference:

Http://www.jb51.net/article/49074.htm

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1643894

PowerShell-background jobs, asynchronous operation instances

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.