PowerShell To Set Folder Permissions

前端 未结 5 821
旧时难觅i
旧时难觅i 2020-12-05 12:35

I am trying to use the \"default\" options in applying folder permissions; by that, I mean that using the \"Full Controll, Write, Read, etc\" in the \'Properties\' for a fol

相关标签:
5条回答
  • 2020-12-05 13:17

    Specifying inheritance in the FileSystemAccessRule() constructor fixes this, as demonstrated by the modified code below (notice the two new constuctor parameters inserted between "FullControl" and "Allow").

    $Acl = Get-Acl "\\R9N2WRN\Share"
    
    $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("user", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    
    $Acl.SetAccessRule($Ar)
    Set-Acl "\\R9N2WRN\Share" $Acl
    

    According to this topic

    "when you create a FileSystemAccessRule the way you have, the InheritanceFlags property is set to None. In the GUI, this corresponds to an ACE with the Apply To box set to "This Folder Only", and that type of entry has to be viewed through the Advanced settings."

    I have tested the modification and it works, but of course credit is due to the MVP posting the answer in that topic.

    0 讨论(0)
  • 2020-12-05 13:17
    $path = "C:\DemoFolder"
    $acl = Get-Acl $path
    $username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    $Attribs = $username, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"
    $AccessRule = New-Object System.Security.AcessControl.FileSystemAccessRule($Attribs)
    $acl.SetAccessRule($AccessRule)
    $acl | Set-Acl $path
    Get-ChildItem -Path "$path" -Recourse -Force | Set-Acl -aclObject $acl -Verbose
    
    0 讨论(0)
  • 2020-12-05 13:18

    Another example using PowerShell for set permissions (File / Directory) :

    Verify permissions

    Get-Acl "C:\file.txt" | fl *
    

    Apply full permissions for everyone

    $acl = Get-Acl "C:\file.txt"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("everyone","FullControl","Allow")
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl "C:\file.txt"
    

    Screenshots:

    Hope this helps

    0 讨论(0)
  • 2020-12-05 13:29

    Referring to Gamaliel 's answer: $args is an array of the arguments that are passed into a script at runtime - as such cannot be used the way Gamaliel is using it. This is actually working:

    $myPath = 'C:\whatever.file'
    # get actual Acl entry
    $myAcl = Get-Acl "$myPath"
    $myAclEntry = "Domain\User","FullControl","Allow"
    $myAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($myAclEntry)
    # prepare new Acl
    $myAcl.SetAccessRule($myAccessRule)
    $myAcl | Set-Acl "$MyPath"
    # check if added entry present
    Get-Acl "$myPath" | fl
    
    0 讨论(0)
  • 2020-12-05 13:33

    In case you had to deal with a lot of subfolders contatining subfolders and other recursive stuff. Small improvment of @Mike L'Angelo:

    $mypath = "path_to_folder"
    $myacl = Get-Acl $mypath
    $myaclentry = "username","FullControl","Allow"
    $myaccessrule = New-Object System.Security.AccessControl.FileSystemAccessRule($myaclentry)
    $myacl.SetAccessRule($myaccessrule)
    Get-ChildItem -Path "$mypath" -Recurse -Force | Set-Acl -AclObject $myacl -Verbose
    

    Verbosity is optional in the last line

    0 讨论(0)
提交回复
热议问题