Hello,
Our script guys have come up with the following script. It is based upon 3 functions:
- GetRolePath - resolves a Security Role name into the ADS path of the directory object that represents the role;
- GetGroupSid - resolves a Distinguished Name (DN) of a group into the group SID;
- CopyRoleAssignment - the core function that Copies the Assignment Scope of an AD group specified by $sourceGroupDN over Security Role $sourceRoleName and assigns the Security Role specified by $destinationRoleName to a security group specified by $destinationGroupDN within the copied Assignment Scope.
At the very end you can find an example of how to use the function.
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
function GetRolePath($name, $securityRolesPath)
{
# Search Security Roles
$searcher = $admService.OpenObject($securityRolesPath, $NULL, $NULL, 0)
$filterPart = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("name", $name)
$searcher.SearchFilter = "(&(objectCategory=adm-Role)$filterPart)"
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
try
{
$searchResult = $searcher.ExecuteSearch()
$objects = $searchResult.FetchAll()
if ($objects.Count -eq 0)
{
Write-Warning "Role $name could not be found"
return $NULL
}
elseif($objects.Count -gt 1)
{
Write-Warning "Found more than one Security Role with name '$name'."
return $NULL
}
return $objects[0].AdsPath
}
finally
{
$searchResult.Dispose()
}
}
function GetGroupSid ($dn)
{
$group = $admService.OpenObject("Adaxes://$dn", $NULL, $NULL, 0)
$sid = New-Object "Softerra.Adaxes.Adsi.Sid" @($group.Get("objectSID"), 0)
return $sid
}
function CopyRoleAssignment($sourceRoleName, $sourceGroupDN, $destinationRoleName, $destinationGroupDN)
{
# Connect to Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")
# Get source role and destination role paths
$securityRolesPath = $admService.Backend.GetConfigurationContainerPath("AccessControlRoles")
$sourceRolePath = GetRolePath $sourceRoleName $securityRolesPath
$destinationRolePath = GetRolePath $destinationRoleName $securityRolesPath
if (($sourceRolePath -eq $NULL) -or ($destinationRolePath -eq $NULL))
{
return
}
# Copy source Security Role Assignment
# Bind to the source Security Role
$sourceRole = $admService.OpenObject($sourceRolePath, $NULL, $NULL, 0)
# Bind to the destination Security Role
$destinationRole = $admService.OpenObject($destinationRolePath, $NULL, $NULL, 0)
# Get the source and destination group SIDs
$sourceGroupSid = GetGroupSid $sourceGroupDN
$destinationGroupSid = GetGroupSid $destinationGroupDN
foreach ($sourceAssignment in $sourceRole.Assignments)
{
if ($sourceAssignment.Trustee -ne $sourceGroupSid)
{
continue
}
$assignment = $destinationRole.Assignments.Create()
$assignment.Trustee = $destinationGroupSid
$assignment.SetInfo()
$destinationRole.Assignments.Add($assignment)
foreach ($item in $sourceAssignment.ActivityScopeItems)
{
$scopeItem = $assignment.ActivityScopeItems.Create()
$scopeItem.BaseObject = $item.BaseObject
$scopeItem.Type = $item.Type
$scopeItem.Inheritance = $item.Inheritance
$scopeItem.Exclude = $item.Exclude
$scopeItem.SetInfo()
$assignment.ActivityScopeItems.Add($scopeItem)
}
}
}
CopyRoleAssignment "Help Desk - User Control" "CN=Help Desk,OU=Groups,OU=Something,DC=domain,DC=com" "Help Desk - Limited - Unlock/Reset" "CN=Help Desk,OU=Groups,OU=Something,DC=domain,DC=com"