Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

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?
Powershell

Avatar of undefined
Last Comment
footech
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I think you've done it, brilliant!!
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

still need to test though
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

can you give me an example of begin process end please.

thank you.
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

apologies I mean begin process end for this scenario
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

should $first be outside of the if statement?
Avatar of footech
footech
Flag of United States of America image

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.
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thank you
Avatar of footech
footech
Flag of United States of America image

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

Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo