PowerShell run commands once in nested foreach

Hi,

I have a code similar to the below:

foreach($a in @(1..3)){
echo $a
foreach ($b in @("a","b","c","d","e")){
echo "Test"

}

}

Open in new window


i want the echo "test" to only happen once within the first foreach.  my real code contains powershell commandlets and i only want to execute these commandlets once per 1..3. so first loop will output 1, then it will go to the next foreach each which gives value a. within a, b, c, d, e. i want to echo test, however when it goes to 2 which is the first foreach, i dont want to echo test anymore. i hope this makes sense.  How can this be achieved?
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Your real-world application could might dictate a better method, but here's one way.
$first = $true
foreach ($a in @(1..3)){
    echo $a
    foreach ($b in @("a","b","c","d","e")){
        If ( $first )
        {
            echo "Test"
            $first = $false
        }
    }
}

Open in new window


In other scenarios, I find using the -Begin, -Process, and -End parameters of ForEach-Object more to my liking.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelly GarciaSenior Systems AdministratorAuthor Commented:
I think you've done it, brilliant!!
Kelly GarciaSenior Systems AdministratorAuthor Commented:
still need to test though
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Kelly GarciaSenior Systems AdministratorAuthor Commented:
can you give me an example of begin process end please.

thank you.
Kelly GarciaSenior Systems AdministratorAuthor Commented:
apologies I mean begin process end for this scenario
Kelly GarciaSenior Systems AdministratorAuthor Commented:
should $first be outside of the if statement?
footechCommented:
In my example, line 8 could also be moved after the If statement (afer line 9), or after the second foreach loop (after line 10) but still with the first foreach loop.
Kelly GarciaSenior Systems AdministratorAuthor Commented:
thank you
footechCommented:
This scenario doesn't really lend itself well to using begin, process, and end.  But here's an example (didn't include end):
1..3 | ForEach-Object -Begin { echo "test" } -Process {
    echo $_
    foreach ($b in @("a","b","c","d","e")){
        # blah
    }
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.