Last active
March 12, 2018 12:58
-
-
Save bergmeister/5a15dca2146fe7a9f789d4bedaa96349 to your computer and use it in GitHub Desktop.
Set-AclRule: Set 'Allow' permissions on a folder (and inherit those permissions) to overcome the limitations of Get-Acl and the Windows API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-AclRule { | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory)] | |
[ValidateScript( {$_.IndexOfAny([System.IO.Path]::GetInvalidPathChars()) -eq -1})] | |
[string] $Path, | |
# The user account | |
[string] $Identity, | |
# The permissions to apply | |
[System.Security.AccessControl.FileSystemRights] $FileSystemRights | |
) | |
if (-not (Test-Path $Path)) { | |
New-Item -ItemType Directory -Path $Path | |
} | |
# See here for why Get-Acl cannot be used: https://stackoverflow.com/a/6646551/1810304 | |
$acl = (Get-Item $Path).GetAccessControl('Access') | |
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Identity, $FileSystemRights.ToString(), 'ContainerInherit,ObjectInherit', 'None', 'Allow') | |
$acl.SetAccessRule($accessRule) | |
Set-Acl -Path $Path -AclObject $acl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment