/ Published in: Windows PowerShell

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
ParallelForEach-Object( [ScriptBlock] $Script = { param([Object] $Data) Write-Output $Data }, [Int] $Threads = 0 ) { begin { $Jobs = @() if ($Threads -eq 0) { Get-WmiObject Win32_Processor | ForEach-Object { $Threads = $Threads + $_.NumberOfLogicalProcessors } } Write-Host "Executing $Threads thread(s)" } process { # Write-Host 'Number of jobs:' $Jobs.Length if ($Jobs.Length -ge $Threads) { # Write-Host 'Waiting for any job to complete' $CompletedJob = Wait-Job $Jobs -Any $Jobs = $Jobs -ne $CompletedJob Receive-Job $CompletedJob Remove-Job $CompletedJob } } end { while ($Jobs.Length -gt 0) { $CompletedJob = Wait-Job $Jobs -Any $Jobs = $Jobs -ne $CompletedJob Receive-Job $CompletedJob Remove-Job $CompletedJob } } }
Comments
