Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected behavior with Get-Job when only one job running
    primarykey
    data
    text
    <p>I'm new to Powershell and I'm having an issue with the Get-Job command. In my script, I'm testing out multi-threading and am doing something like:</p> <pre><code>$Program = { "Thread " + $args[0]; Start-Sleep 5; } Start-Job $Program -ArgumentList @($i) | Out-Null </code></pre> <p>The Start-Job call is actually in a loop in which I'm creating multiple jobs. Below this, I have:</p> <pre><code>Get-Job "Jobs Running: " + $(Get-Job -State Running).count </code></pre> <p>If there are multiple jobs running, I will get output like:</p> <pre><code>Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 2201 Job2201 Running True localhost ... 2199 Job2199 Running True localhost ... 2197 Job2197 Running True localhost ... 2195 Job2195 Running True localhost ... 2193 Job2193 Completed True localhost ... 2191 Job2191 Completed True localhost ... 2189 Job2189 Completed True localhost ... 2187 Job2187 Completed True localhost ... Jobs Running: 4 </code></pre> <p>But if there is only one job running, it seems that <code>$(Get-Job -State Running).count</code> isn't returning anything:</p> <pre><code>Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 2207 Job2207 Running True localhost ... Jobs Running: </code></pre> <p>As you can see, there is one job running, but <code>$(Get-Job -State Running).count</code> doesn't return anything. Any idea what's happening here? To me, it looks like that if there are multiple jobs, <code>$(Get-Job -State Running)</code> returns a collection of jobs which has the .count property, whereas if there is only one job, it returns just that job, and doesn't have the .count property. If this is the case (or if I'm doing something wrong), what command should I be using to get my expected result of <code>$(Get-Job -State Running).count == 1</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload