Solved PowerShell start-job wait-job,running 10 jobs three at a tim

Microsoft Visio professional 2003 (full...
February 25, 2015 at 10:23:00
Specs: Windows 7, Dell percision 530
Before I post my thre page question. Des anyone have experience with powershell startjob function.

Specifically I want to start a bunch of jobs , say 10 , I only want three at a time to run .

Anyone have experience with this?

Learning in progress..........


See More: PowerShell start-job wait-job,running 10 jobs three at a tim

Report •

#1
February 28, 2015 at 16:40:55
✔ Best Answer
Well, that's sufficiently vague that any answer will be basically pointless. Good job!

If you want to do the same thing, but on a different input, and you know the operation will take roughly the same amount of time, regardless of the input, the easiest method is to just split your list into however many parts you want, and pass along those along as arguments. It's my preferred method for remotely querying a large number of servers.

For instance, say you're really interested in getting an MD5 hash of your running executables.

Get-Process | 
where { ![String]::IsNullOrEmpty($_.Path) } |
select -Unique Path |
foreach -Begin { $md5 = New-Object Security.Cryptography.MD5CryptoServiceProvider } { 
  New-Object PSObject -Property @{ Path = $_.Path
    MD5 = [BitConverter]::ToString($md5.ComputeHash([IO.File]::ReadAllBytes($_.Path))) 
                                 }
} | 
ft -auto

Now let's pretend this isn't I/O bound, and say you wanted to generate the hashes 4 at a time. We'll keep the collection of paths the same, but this time we'll save them to an array of an array of strings. Then we'll feed those arrays into Start-Job. We'll wait for all 4 instances to complete, dump the results to screen, and finally clean up by removing the jobs.

[int]$chunks = 4
[string[][]]$procs = New-Object string[][] $chunks
[int]$i = 0

Get-Process | 
where { ![String]::IsNullOrEmpty($_.Path) } |
select -Unique Path |
foreach { $procs[$i] += , $_.Path; $i = ($i + 1) % $chunks }

foreach ($p in $procs) {
  Start-Job -ArgumentList $p -ScriptBlock {
    $args | 
    foreach -Begin { $md5 = New-Object Security.Cryptography.MD5CryptoServiceProvider } { 
      New-Object PSObject -Property @{ Path = $_
       MD5 = [BitConverter]::ToString($md5.ComputeHash([IO.File]::ReadAllBytes($_))) 
      }
    }
  } |
  Out-Null
}

Wait-Job * | 
Receive-Job |
select -p * -exc RunspaceId | 
ft -auto

Remove-Job *

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question