views:

101

answers:

2

I have a powershell script to do some batch processing on a bunch of images and I'd like to do some parallel processing. Powershell seems to have some background processing options such as start-job, wait-job, etc, but the only good resource I found for doing parallel work was writing the text of a script out and running those (PowerShell Multithreading)

Ideally, I'd like something akin to parallel foreach in .net 4.

Something pretty seemless like:

foreach-parallel -threads 4 ($file in (Get-ChildItem $dir))
{
   .. Do Work
}

Maybe I'd be better off just dropping down to c#...

+5  A: 

There is an example of how to execute parallel jobs in Powershell 2 here. Are you looking for something more 'integrated' than this?

# Loop through the server list

Get-Content "ServerList.txt" | %{

  # Define what each job does

  $ScriptBlock = {
    Test-Path "\\$_\c$\Something"
    Start-Sleep 60
  }

  # Execute the jobs in parallel

  Start-Job $ScriptBlock
}

Get-Job

# Wait for it all to complete

While (Get-Job -State "Running")
{
  Start-Sleep 10
}

# Getting the information back from the jobs

Get-Job | Receive-Job
Steve Townsend
This looks like exactly what I was looking for thanks. Off to try it out...
Alan Jackson
A: 

Backgrounds jobs are expensive to setup and are not reusable. PowerShell MVP Oisin Grehan has a good example of PowerShell multi-threading http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx

(10/25/2010 site is down):

I'e used adapted Oisin script for use in a data loading routine here:

http://rsdd.codeplex.com/SourceControl/changeset/view/a6cd657ea2be#Invoke-RSDDThreaded.ps1

Chad Miller