Question

I am curious as to why Start-Job increments in twos. My worry is that I am doing something wrong that makes the ID of a new job jump by 2.

Start-Job -ScriptBlock {Get-WinEvent -LogName system -MaxEvents 1000} 

Results as shown by Get-Job

Id Name   State     HasMoreData Command                                                                                                                                                                     
-- ----   -----     ----------- -------                                                                                                                                                                     
 2 Job2   Completed       False Get-WinEvent -LogName system -MaxEvents 1000                                                                                                                                
 4 Job4   Completed       False Get-WinEvent -LogName system -MaxEvents 1000                                                                                                                                
 6 Job6   Completed        True Get-WinEvent -LogName system -MaxEvents 1000  

Question: Can you control the Start-Job Id increments, or force them to be just 1?

Était-ce utile?

La solution

Each time you start a job, it consists of a parent job and one or more child jobs. If you run get-job | fl you'll see the child jobs, and you'll see that their names are the "missing" odd numbered names.

Autres conseils

@1.618 give the right answer, here are some more details :

Start-Job -ScriptBlock {Get-Process}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
2      Job2            BackgroundJob   Running       True            localhost            Get-Process   

Get-Job | fl *


State         : Completed
HasMoreData   : True
StatusMessage : 
Location      : localhost
Command       : Get-Process
JobStateInfo  : Completed
Finished      : System.Threading.ManualResetEvent
InstanceId    : 49a67ca4-840b-49ec-b293-efa9303e38bb
Id            : 2
Name          : Job2
ChildJobs     : {Job3}
PSBeginTime   : 03/03/2014 20:43:54
PSEndTime     : 03/03/2014 20:44:00
PSJobTypeName : BackgroundJob
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}

get-job -IncludeChildJob

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
2      Job2            BackgroundJob   Completed     True            localhost            Get-Process              
3      Job3                            Completed     True            localhost            Get-Process

Here is why, when you start a job, powershell create two jobs ?

Windows PowerShell jobs created through Start-Job always consist of a parent job and a child job. The child job does the actual work. If you were running the job against a number of remote machines by using Invoke-Command and its –AsJob parameter, you would get one child job per remote machine.

When you manage jobs, anything you do to the parent job is automatically applied to any child jobs. Removing or stopping the parent job performs the same action on the child jobs. Getting the results of the parent job means you get the results of all the child jobs.

You can access the child jobs directly to retrieve their data, n a simple job, as in the example, you can access the data through the parent or child jobs :

Receive-Job -Id 2 -Keep
Receive-Job -Id 3 -Keep

When you have multiple child jobs, its usually easier to access the child jobs in turn:

$jobs = Get-Job -Name Job2 | select -ExpandProperty ChildJobs
foreach ($job in $jobs){Receive-Job -Job $job -Keep}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top