Skip to content

Instantly share code, notes, and snippets.

@jeffpatton1971
Last active August 29, 2015 14:03
Show Gist options
  • Save jeffpatton1971/d838bedcb4025d804106 to your computer and use it in GitHub Desktop.
Save jeffpatton1971/d838bedcb4025d804106 to your computer and use it in GitHub Desktop.
Copy AD ACL's for one principal to another principal
#
# Copy AD ACL Rules
#
param
(
$adPath,
$secPrincipal,
$newPrincipal
)
$Permissions = ([adsi]$adPath).ObjectSecurity;
if ($Permissions)
{
$Rules = $Permissions.Access |Where-Object -Property IdentityReference -eq $secPrincipal;
$IdentityReference = New-Object System.Security.Principal.NTAccount($newPrincipal);
$NewRules = @()
foreach ($Rule in $Rules)
{
$NewRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule(
$IdentityReference,
$Rule.ActiveDirectoryRights,
$Rule.AccessControlType,
$Rule.ObjectType,
$Rule.InheritanceType,
$Rule.InheritedObjectType);
$NewRules += $NewRule;
$Permissions.SetAccessRule($NewRule);
}
}
return $NewRules;
else
{
Write-Host "No permissions returned from $($adPath), please verify that you have typed the path in properly";
}