Skip to content

Instantly share code, notes, and snippets.

@jberezanski
Last active July 14, 2017 09:37
Show Gist options
  • Save jberezanski/359d9de0be70a3dc8633 to your computer and use it in GitHub Desktop.
Save jberezanski/359d9de0be70a3dc8633 to your computer and use it in GitHub Desktop.
function Grant-Permissions
{
[CmdletBinding()] Param([string] $Path, [hashtable] $Grants, [switch] $BlockInheritance, [switch] $CopyInheritedRights, [switch] $RemoveAllExistingPermissions, [string] $Inheritance = 'ObjectInherit,ContainerInherit')
Resolve-Path $Path
$acl = (Get-Item $Path).GetAccessControl('Access')
if ($BlockInheritance) { $acl.SetAccessRuleProtection($true, $false) }
if ($RemoveAllExistingPermissions) { $acl.Access | % { $acl.RemoveAccessRuleAll($_) } }
$Grants.GetEnumerator() | % { $acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule $($_.Key),$($_.Value),$Inheritance,'None','Allow')) }
(Get-Item $Path).SetAccessControl($acl)
(Get-Acl $Path).Access | Format-Table -AutoSize | Out-String
}
# example
Grant-Permissions -Path C:\Test -Grants @{'BUILTIN\Users' = 'ReadAndExecute'; 'BUILTIN\Administrators' = 'FullControl'; 'NT AUTHORITY\SYSTEM' = 'FullControl'} -BlockInheritance -RemoveAllExistingPermissions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment