Skip to content

Instantly share code, notes, and snippets.

@bergmeister
Last active March 12, 2018 12:58
Show Gist options
  • Save bergmeister/5a15dca2146fe7a9f789d4bedaa96349 to your computer and use it in GitHub Desktop.
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
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