Using a Counter Variable in a PowerShell Workflow Foreach -Parallel Loop

As I’m sure most of my blog readers are aware, I competed in the advanced track of the Scripting Games this year and ended up doing quite well. Two first place finishes and three second places finishes with the official judges and four first place crowd score finishes. I also ended up being the overall winner of the advanced track.

A few days ago someone on twitter asked me as the winner of the Scripting Games advanced track, what I would do with it? I plan to use it as a tool. I’ll continue doing the same thing I’ve been doing of learning more and trying to spread the word about PowerShell to others as well as sharing my knowledge of what I learn. Maybe as a Scripting Games winner, a few more people will listen.

Today I’ll start the first of many blogs to come in a series that shares information about what I learned during the Scripting Games. During event six, one of the goals was to name servers as SERVER1, SERVER2, etc. I started out doing something like this in a foreach loop:

workflow-counter1

Then I ended up using a workflow with foreach -parallel and that didn’t work out so well:

workflow-counter2

Here’s what I discovered was the solution to return the desired results in a workflow foreach -parallel loop:

workflow-counter3

I ended up not using this particular method of naming the servers, but this was something that wasn’t easy to figure out and there isn’t much documentation in books or on the Internet about this so I thought it was worth sharing and in six months when I need to do something like this again, I can search my blog to figure out how I did it.

Update: Be sure to see the comments on this blog article. As the tip from Jeffery Hicks in the comments states, you can’t count on the results from the workflow being sequential.

µ

3 Comments

  1. Jeffery Hicks (@JeffHicks)

    Scope is always important in PowerShell and even more so in a workflow. You can’t assume that you can share variables across workflow activities. Using the workflow: prefix is one way. It is sorta-kinda like setting a global variable for the workflow. But I would think this should be an exception rather than the rule.

    Reply
  2. Jeffery Hicks (@JeffHicks)

    In your example, defining a server name is no big deal but even though you are using a range of numbers, there’s no guarantee that activities run in parallel will run sequentially. I tested with a larger number range and notice that the server names are not sequential.

    SERVER12
    SERVER13
    SERVER15
    SERVER16
    SERVER14
    SERVER18
    SERVER20
    SERVER19
    SERVER21
    SERVER17

    I know you were testing using a counter, But in your example, you already have an integer you can use. Your counter, in this example, is duplicative.

    workflow test2 {
    foreach -parallel ($n in 1..100){
    $a = “SERVER$n”
    $a
    }
    }

    Reply
  3. Jonas

    Guess you could use:
    foreach ($_ in 1..10) {
    $a = “Server”
    “$a$_”
    }
    if you wanted to 🙂

    Reply

Leave a Reply

%d bloggers like this: