Avatar of Itxx
Itxx
 asked on

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

Powershell* NTFS

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
footech

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Qlemo

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Itxx

ASKER
It works like a charm!

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

Thanks for the help.
Qlemo

People need a break, foreach doesn't :D.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23