{"id":264,"date":"2020-06-01T19:39:15","date_gmt":"2020-06-01T18:39:15","guid":{"rendered":"http:\/\/192.168.8.14\/?p=264"},"modified":"2020-06-01T19:39:15","modified_gmt":"2020-06-01T18:39:15","slug":"simple-acl-functions","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=264","title":{"rendered":"Simple ACL functions"},"content":{"rendered":"\n<p>Here is a set of simple functions for basic management of directory permissions or Access Control Lists. <\/p>\n\n\n\n<p>when setting up file shares I tend to it in very few unique steps. Break inheritance while keeping ACLs, Add\/Remove\/Modify users or groups. So I have 4 powershell functions to simply complete these tasks.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nfunction acl-Disableinheritance\n{\n    &#x5B;CmdletBinding()]\n    param\n    (\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;ValidateScript({test-path $_})]&#x5B;string]$PathName\n\n    )\n\n    # get ACLs\n    $FolderACL  = Get-Acl $PathName\n\n    # protect the acls from inheritace (break inheritace) , allow ACLs to inherit down.\n    $FolderACL.SetAccessRuleProtection($true,$true)\n\n    # write new ACL object back to directory\n    Set-Acl $PathName -AclObject $FolderACL\n    if ($?)\n    {\n        return $true\n    }else{\n        return $false\n    }\n}\n\n\nfunction acl-RemoveItem\n{\n    &#x5B;CmdletBinding()]\n    param\n    (\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;ValidateScript({test-path $_})]&#x5B;string]$PathName,\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;string]$UserOrGroup\n\n    )\n\n    # get ACLs\n    $FolderACL  = Get-Acl $PathName\n\n    # loop through each User (or Group) called access rules and make $AccessRuleToDelete the the rule to delate.\n    foreach ($AccessRule in $FolderACL.access)\n    {\n        if ($AccessRule.IdentityReference -eq $UserOrGroup)\n        {\n            $AccessRuleToDelete = $AccessRule\n        }\n    }\n\n    # check the rule to delete exists\n    if ($AccessRuleToDelete)\n    {\n        # remove the access rule from the directory ACL object\n        $FolderACL.RemoveAccessRule($AccessRuleToDelete)\n\n        # write new ACL object back to directory\n        Set-Acl $PathName -AclObject $FolderACL\n        return $true\n    }else{\n        write-host &quot;$UserOrGroup not found&quot; -ForegroundColor Red\n        return $false\n    }\n}\n\nfunction acl-AddItem\n{\n    &#x5B;CmdletBinding()]\n    param\n    (\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;ValidateScript({test-path $_})]&#x5B;string]$PathName,\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;string]$UserOrGroup,\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;ValidateSet(&quot;FullControl&quot;,&quot;Modify&quot;,&quot;ReadAndExecute&quot;)]&#x5B;string]$Permission\n\n    )\n\n    # get ACLs\n    $FolderACL  = Get-Acl $PathName\n\n    # create new access rule object\n    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($UserOrGroup,$Permission,&#039;ContainerInherit,ObjectInherit&#039;,&#039;None&#039;,&#039;Allow&#039;)\n\n    # add new access rule object to the directory ACL object\n    $FolderACL.SetAccessRule($AccessRule)\n    if ($?)\n    {\n        # write new ACL object back to directory\n        Set-Acl $PathName -AclObject $FolderACL\n        if ($?){return $true}\n\n    }else{\n        Write-Host &quot;User or Group $UserOrGroup not found!&quot; -ForegroundColor Red\n        return $false\n    }\n}\n\nfunction acl-getItem\n{\n    &#x5B;CmdletBinding()]\n    param\n    (\n        &#x5B;Parameter(Mandatory = $true)]  &#x5B;ValidateScript({test-path $_})]&#x5B;string]$PathName,\n        &#x5B;switch]$ShowSync\n    )\n\n    # get ACLs\n    $FolderACL  = Get-Acl $PathName\n\n    $Result = @()\n\n    # loop though each access rule and make a nice simple array\n    foreach ($AccessRule in $FolderACL.access)\n    {\n        $tmpObj = &#039;&#039; | select UserOrGroup , Permission , IsInherited\n        $tmpObj.UserOrGroup = $AccessRule.IdentityReference\n        if ($ShowSync)\n        {\n            $tmpObj.Permission = $AccessRule.FileSystemRights\n        }else{\n            # remove the Synchronize ACL from the list as its normaly hidden.\n            &#x5B;string]$RawPerm = $AccessRule.FileSystemRights\n            $PermArray = $RawPerm.Split(&#039;,&#039;)\n            $NewPermArray = $PermArray | where {$_ -notmatch &quot;Synchronize&quot;}\n            $tmpObj.Permission = $NewPermArray\n        }\n\n        $tmpObj.IsInherited = $AccessRule.IsInherited\n        $Result += $tmpObj\n    }\n    if ($Result)\n    {\n        return $Result\n    }else{\n        return $null\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>Put those functions at the start of your program and you will have the following functions<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>acl-Disableinheritance [Path]   Will disable inheritance and copy the ACLs.<\/li><li>acl-RemoveItem -PathName [path] -UserOrGroup [user]   will remove the user or group permission from the directory.<\/li><li>acl-AddItem -PathName [path] -UserOrGroup [user] -Permission Modify|ReadAndExecute|FullControl   will create or modify a user or group ACL.<\/li><li>acl-getItem -PathName [path]   will return a simple list of ACLs <\/li><\/ul>\n\n\n\n<p>To keep these functions simple, there is no ownership function or any way to set granular permissions or set denys.<\/p>\n\n\n\n<p>The functions will return $true if successful (with the exception of  acl-getItem that will return an array) and $false if the function fails.<\/p>\n\n\n\n<p>To quickly permission up a simple shared file store with the following directory structure.<\/p>\n\n\n\n<p>\\nas01\\cifs<br>\\nas01\\cifs\\sales<br>\\nas01\\cifs\\marketing<br>\\nas01\\cifs\\IT<\/p>\n\n\n\n<p>I would call the functions as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nacl-RemoveItem  -PathName &quot;\\\\nas01\\cifs&quot; -UserOrGroup &quot;Everyone&quot;\n\nacl-Disableinheritance -PathName &quot;\\\\nas01\\cifs\\sales&quot;\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\sales&quot; -UserOrGroup &quot;domain\\ACL-Sales-R&quot; -Permission ReadAndExecute\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\sales&quot; -UserOrGroup &quot;domain\\ACL-Sales-RW&quot; -Permission Modify\n\nacl-Disableinheritance -PathName &quot;\\\\nas01\\cifs\\marketing&quot;\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\marketing&quot; -UserOrGroup &quot;domain\\ACL-marketing-R&quot; -Permission ReadAndExecute\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\marketing&quot; -UserOrGroup &quot;domain\\ACL-marketing-RW&quot; -Permission Modify\n\n\nacl-Disableinheritance -PathName &quot;\\\\nas01\\cifs\\IT&quot;\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\IT&quot; -UserOrGroup &quot;domain\\ACL-IT-R&quot; -Permission ReadAndExecute\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\IT&quot; -UserOrGroup &quot;domain\\ACL-IT-RW&quot; -Permission Modify\nacl-AddItem  -PathName &quot;\\\\nas01\\cifs\\IT&quot; -UserOrGroup &quot;domain\\ACL-IT-Admin&quot; -Permission FullControl\n<\/pre><\/div>\n\n\n<p>There you go, a quick and easy way to apply ACLs. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a set of simple functions for basic management of directory permissions or Access Control Lists. when setting up file shares I tend to it in very few unique steps. Break inheritance while keeping ACLs, Add\/Remove\/Modify users or groups. So I have 4 powershell functions to simply complete these tasks. Put those functions at&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[39,42,41,17,40],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-acl","tag-directory","tag-folder","tag-function","tag-permissions"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":6,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions\/270"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}