I have a directory which I want to go through recursively and set permissions on all the folders. So the order of operations should be:
I tried the below code, but I am getting the error
Cannot set the ACL because the method that it needs to invoke, SetSecurityDescriptor, does not exist.
foreach ($folder in Get-ChildItem -Path c:\perms -Recurse -Directory) {
$AccessRule = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("user", "FullControl", "ContainerInherit,ObjectInherit", "InheritOnly", "Allow")
$acl = Get-Acl $folder
$acl.SetAcccessRule($AccessRule)
Set-Acl -Path $folder.FullName -AclObject $acl
}
I got rid of the error message, and it added the ACL, but I want to basically remove all ACLs from the folder and add new ones.
I updated my script to look like this:
$acl = Get-Acl -Path "c:\perms"
$acl.SetAccessRuleProtection($true,$false)
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) | Out-Null }
$ace = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("user", "FullControl", "ContainerInherit,ObjectInherit", "InheritOnly", "Allow")
$acl.AddAccessRule($ace)
Set-Acl -Path "c:\perms" -AclObject $acl
If I want to add multiple $ace
, is it just a matter of declaring $ace2
, $ace3
and then calling $acl.AddAccessRule($ace2)
, $acl.AddAccessRule($ace3)
.
The chmod command with the -R options allows you to recursively change the file's permissions. To recursively set permissions of files based on their type, use chmod in combination with the find command. If you have any questions or feedback, feel free to leave a comment.
To use Set-Acl , use the Path or InputObject parameter to identify the item whose security descriptor you want to change. Then, use the AclObject or SecurityDescriptor parameters to supply a security descriptor that has the values you want to apply. Set-Acl applies the security descriptor that is supplied.
One of the options to change multiple files is to run chmod recursive with the -R (recursive, and not the capital) option. The recursive option will change the permissions for all the files, including those under sub-directories, inside a given path. 1. Consider the following command, chmod -R a=r,u=rwx my_dir .
Use SetAccessRuleProtection()
to disable inheritance and remove inherited ACEs:
$acl.SetAccessRuleProtection($true, $false)
Use RemoveAccessRule()
to remove existing (non-inherited) ACEs:
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) | Out-Null }
Use AddAccessRule()
to add new ACEs:
$ace = New-Object Security.AccessControl.FileSystemAccessRule "user", ...
$acl.AddAccessRule($ace)
...
Do this only for the topmost folder. Leave inheritance enabled everywhere below, so your changes are propagated automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With