goal is to copy groups from one user to another during the crete user process.
I created a variable on the create user form to input the UPN of the source user
i refernce that in the script to get the groups this source user is a member of
here is the script. below is also they error.
$sourceUserDNParamName = "%adm-CustomAttributeText36%" # TODO: modify me
$replaceGroups = $True # TODO: modify me
# Bind to the source user
#$sourceUserDN = $Context.GetParameterValue($sourceUserDNParamName)
$sourceUser = $Context.BindToObjectByDNEx("$sourceUserDNParamName", $True)
# Get groups to add
$groupGuidsToAdd = New-Object "System.Collections.Generic.HashSet[System.Guid]"
$sourceUser.GetEx("adm-DirectMemberOfGuid") | %%{[void]$groupGuidsToAdd.Add([Guid]$_)}
# Get current groups
$currentGroupGuids = New-Object "System.Collections.Generic.HashSet[System.Guid]"
$Context.TargetObject.GetEx("adm-DirectMemberOfGuid") | %%{[void]$currentGroupGuids.Add([Guid]$_)}
# Update groups
foreach ($guidBytes in $groupGuidsToAdd)
{
$guid = [Guid]$guidBytes
if ($currentGroupGuids.Remove($guid))
{
continue
}
$group = $Context.BindToObjectEx("Adaxes://<GUID=$guid>", $True)
$group.Add($Context.TargetObject.AdsPath)
}
if ($replaceGroups)
{
# Get the primary group ID
$primaryGroupId = $Context.TargetObject.Get("primaryGroupID")
foreach ($guidBytes in $currentGroupGuids)
{
$guid = [Guid]$guidBytes
$group = $Context.BindToObjectEx("Adaxes://<GUID=$guid>", $True)
# Skip the group if it is the user's Primary Group
if ($group.Get("primaryGroupToken") -eq $primaryGroupId)
{
continue
}
$group.Remove($Context.TargetObject.AdsPath)
}
}