We help IT Professionals succeed at work.

Nested ForEach in Powershell

Itxx
Itxx asked
on
2,304 Views
Last Modified: 2017-03-22
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
It works like a charm!

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

Thanks for the help.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
People need a break, foreach doesn't :D.