Nested ForEach in Powershell

Hi all,

I'm trying this relative simple script but I think I'm not using the break function right.

First, I need to iterate through list 1(pathlist.txt). For every object in list 1, I need to iterate through list2 (hvservers.txt) and apply NTFS permissions to the paths from list1 and add the accounts from list2.

The script goes like this:
Import-Module NTFSSecurity

$pathlist = Get-Content 'C:\itxx\hvfolderpermissions\pathlist.txt'
$hvlist = Get-Content 'C:\itxx\hvfolderpermissions\hvservers.txt'


ForEach ($Path In $pathlist) {
:next

ForEach ($HVserver In $hvlist) {

Add-NTFSAccess -Path $Path -account $HVserver -AccessRights FullControl

break:next


}

}

Open in new window

LVL 1
ItxxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
In your example, the syntax would be:
ForEach ($Path In $pathlist) {
  :next ForEach ($HVserver In $hvlist) {
    Add-NTFSAccess -Path $Path -account $HVserver -AccessRights FullControl
    break next
  }
}

Open in new window


However, based on your description, the break statement here is not needed.  Without the break statement it will iterate through items in List1, adding all items in List2.  With the break statement inserted, it would add the first permission in List2, break out of the foreach, and then continue on to the next item in List1.
0

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
footech is correct - you do not use break here. In general, you never use it, with very few execeptions.
But usually you try to use pipelining at least for the innermost or outermost loop. I would rewrite it hence:
ForEach ($Path In $pathlist) {
    $hvlist | % { Add-NTFSAccess -Path $Path -account $_ -AccessRights FullControl }
}

Open in new window

0
ItxxAuthor Commented:
It works like a charm!

I was really convinced that nested ForEach would need a break.

Thanks for the help.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
People need a break, foreach doesn't :D.
0
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.