I have two files, one with a list of the security groups and one with the corresponding folder path. All I need to do is loop through these files and apply the correct security group recursive RW access to the correct folder.
So security group on line 1 would apply to the folder on line 1.
Powershell script:
foreach ($group in gc c:\temp\securitygroups.txt) {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule ($group, 'Modify','ContainerInherit,ObjectInherit', 'None', 'Allow')
foreach ($folder in gc c:\temp\folders.txt) {
$acl = Get-Acl $folder
$acl.SetAccessRule($rule)
Set-Acl $folder $acl
}
}
securitygroups.txt
:
securitygroup1 securitygroup2 securitygroup3 securitygroup4 securitygroup5 securitygroup6 securitygroup7 securitygroup8 securitygroup9 securitygroup10
folders.txt
:
D:\shares\projects\project1 D:\shares\projects\project2 D:\shares\projects\project3 D:\shares\projects\project4 D:\shares\projects\project5 D:\shares\projects\project6 D:\shares\projects\project7 D:\shares\projects\project8 D:\shares\projects\project9 D:\shares\projects\project10
At the moment every security group in securitygroups.txt is being added to each folder in the list, this is not what I want, I want securitygroup1
adding to project1
, securitygroup2
adding to project2
etc.
Read both files into variables, then use a for
loop to iterate over both arrays at the same time:
$groups = Get-Content 'c:\temp\securitygroups.txt'
$folders = Get-Content 'c:\temp\folders.txt'
for ($i=0; $i -lt $folders.Count; $i++) {
$rule = New-Object Security.AccessControl.FileSystemAccessRule ($groups[$i], 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$acl = Get-Acl $folders[$i]
$acl.SetAccessRule($rule)
Set-Acl $folders[$i] $acl
}