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?
The $() is the subexpression operator. It causes the contained expressions to be evaluated and it returns all expressions as an array (if there is more than one) or as a scalar (single value).
The assignment by addition operator += either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection).
The Start-Job cmdlet starts a PowerShell background job on the local computer. A PowerShell background job runs a command without interacting with the current session. When you start a background job, a job object returns immediately, even if the job takes an extended time to finish.
The following command starts a job object and saves the resulting job object in the $job variable. Beginning in PowerShell 6.0, you can use the background operator ( & ) at the end of a pipeline to start a background job.
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.
@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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With