The script actually works as intended, with large files though and no progress bar it seemed like it wasn't doing anything. Applied an echo and progress bar.
Powershell - SetAccessRuleProtection not working in script
-
13-07-2023 - |
Question
I have a script that copies folders to computers on the network but sometimes I need to set permissions to them. I tested this script below and it worked for one machine but it won't work for the others, it just hangs. Any idea why this wouldn't work?
# This is the file that contains the list of computers you want
# to modify permissions to. Change this path IAW your folder structure.
$computers = gc "C:\scripts\computers.txt"
# This is the folder you want to modify.
$source = "c$\AEM"
foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
"Modifying for $computer"
$acl = Get-Acl \\$computer\$source
$acl.SetAccessRuleProtection($True, $True)
# This is where you set the permissions you want, Modify the group (the first object) and the rights (the second object) as needed.
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl \\$computer\$source $acl
} else {
"$computer is not online"
}
}
It's pretty simple in terms of what it does. It goes out to the remote machine and leaves inheritance so I'm not overwriting anything. Then it adds permissions I have specified and should loop back until all the computers I listed in the text file have been modified. Works great when I do a copy, worked great on a single machine, but when I try it on other machines in one of our training rooms it fails on the first one. Am I missing something? I have administrative access across the network so it's not that I don't have permission to do this.
Solution