Hi Dave, thanks a lot, I didn’t think of that.
To get the replace to work, I had to omit the regexp portionm like this:
$newName = $sourceAccount.Value -replace $sourceOUName, $destOUName
Echoing out “$sourceAccount.Value” in quotes gave me the DN of the OU instead of just the name, echoing $sourceAccount.Value without quotes gave me the name instead, but not entirely sure if that was the problem with the regexp too.
Anyway, If I print out the destAce after the replace it looks good, the portion of the groupname has been replaced:
---------- SOURCE ACL v ----------
ActiveDirectoryRights : WriteProperty
InheritanceType : None
ObjectType : 00000000-0000-0000-0000-000000000000
InheritedObjectType : 00000000-0000-0000-0000-000000000000
ObjectFlags : None
AccessControlType : Allow
IdentityReference : MYDOMAIN\Admins@4077.dk
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
---------- DESTINATION ACL v ----------
ActiveDirectoryRights : WriteProperty
InheritanceType : None
ObjectType : 00000000-0000-0000-0000-000000000000
InheritedObjectType : 00000000-0000-0000-0000-000000000000
ObjectFlags : None
AccessControlType : Allow
IdentityReference : MYDOMAIN\Admins@4204.dk
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
But calling Set-ADOrganizationalUnit on the $destOU in the end, either inside or after the foreach loop, doesn’t write the ACL to the destination OU when checking in AD Users and Computers, security properties of the OU (and no errors).
Set-ADOrganizationalUnit -instance $destOU
Do you have any more pointers on how to apply the new ACLs?
And do you have an idea for how to remove the already non-inherited ACLs on the destOU easily?
The slightly modified script so far:
cls
$sourceOU = Get-ADOrganizationalUnit -Identity 'OU=4077.dk,OU=Hosting,DC=mycorp,DC=dk' -Properties nTSecurityDescriptor -ErrorAction Stop
$destOU = Get-ADOrganizationalUnit -Identity 'OU=4204.dk,OU=Hosting,DC=mycorp,DC=dk'-Properties nTSecurityDescriptor -ErrorAction Stop
# You might want to clear the destination ACL of non-inherited ACEs before starting this loop
foreach ($sourceAce in $sourceOU.nTSecurityDescriptor.Access)
{
if ($sourceAce.IsInherited) { continue }
"---------- SOURCE ACL v ----------"
$sourceAce
$identityReference = $null
# try
# {
$sourceAccount = $sourceAce.IdentityReference.Translate([System.Security.Principal.NTAccount])
$newName = $sourceAccount.Value -replace $sourceOU.Name, $destOU.Name
$identityReference = [System.Security.Principal.NTAccount]$newName
# }
# catch
# {
# You may want to log an error if you can't translate the SID to User\Group form
# }
$destAce = $destOU.nTSecurityDescriptor.AccessRuleFactory($identityReference,
$sourceAce.ActiveDirectoryRights,
$sourceAce.IsInherited,
$sourceAce.InheritanceFlags,
$sourceAce.PropagationFlags,
$sourceAce.AccessControlType,
$sourceAce.ObjectType,
$sourceAce.InheritedObjectType)
"---------- DESTINATION ACL v ----------"
$destAce
# This statement will throw an error if the destination identity doesn't exist.
$destOU.nTSecurityDescriptor.AddAccessRule($destAce)
Set-ADOrganizationalUnit -instance $destOU
}
Thanks a lot - I really appreciate it!
Nicolaj