Parallel processing with Foreach-Object

Welcome Forums General PowerShell Q&A Parallel processing with Foreach-Object

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #162096

    Participant
    Topics: 1
    Replies: 2
    Points: 15
    Rank: Member

    I have a fairly simple request but it doesn't seem to work

    Essentially I have an xml file which has n elements.

    I iterate over the elements using Foreach-Object

    As I am processing each element I use Start-Process to kick off a task.

    I check for the last item in the collection and when I hit it, the same Start-Process as the others, invokes but this time with a -Wait.

    Once all have been completed, I would expect the -End block to run but it never does

    I can post code if you wish but it is a pretty straight forward thing, i think

  • #162101

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    when you use -Wait, Start-Process waits for the process to complete. You can use -PassThru instead of wait and catch the output in an array. Once the iteration is completed use the array with Wait-Process cmdlet.

  • #162149

    Participant
    Topics: 1
    Replies: 2
    Points: 15
    Rank: Member

    ok, let me play with that.  However, I do want it to wait.  I would have thought when the final enumeration completed, the one with the wait, it would have run the finish block

    Here is a gist just to show the full thing.

    https://gist.github.com/tstauffer/e42b4be802634505d3cfc4b39c03ad8a

     

  • #162155

    Participant
    Topics: 1
    Replies: 2
    Points: 15
    Rank: Member

    Initial testing seems to work.  But now I am really curious why the first one didn't work.  Just a teaching moment please

The topic ‘Parallel processing with Foreach-Object’ is closed to new replies.