Thanks for the hint. That worked as expected.
Here the full script which copies the groups - filered by name. Just create as meantioned the business rule for the group name. If an approval is triggered, it will be shown as warning in execution log.
$sourceUserDN = "%seeAlso%"
$Context.LogMessage("User: " + $sourceUserDN, "Information")
# Bind to the source user
try
{
#$sourceUserDN = $Context.TargetObject.Get($source)
$sourceUser = $Context.BindToObjectByDN($sourceUserDN)
}
catch
{
$Context.LogMessage("The user to copy properties from is not specified", "Warning")
return
}
# Get group memberships
try
{
$groupGuidsInBytes = $sourceUser.GetEx("adm-DirectMemberOfGuid")
}
catch
{
$Context.LogMessage($sourceUser.Name + " is not a member of any groups", "Information")
return
}
# Get the ID of the target user's primary group
$primaryGroupId = $Context.TargetObject.Get("primaryGroupID")
# Add target user to groups
$Context.LogMessage("Adding the user to groups:", "Information")
foreach ($groupGuidBytes in $groupGuidsInBytes)
{
$groupGuid = New-Object "System.Guid" (,$groupGuidBytes)
$groupGuid = $groupGuid.ToString("B")
$group = $Context.BindToObjectEx("Adaxes://<GUID=$groupGuid>", $True) # TRUE Bedeutet dass Business Rules ziehen
# Skip the group if it is the primary group for the user
if ($group.Get("primaryGroupToken") -eq $primaryGroupId)
{
continue
}
# Kopiere OG Gruppen // Organisation
if ($group.Get("name") -like "OG *" -or $group.Get("name") -like "OG_*") {
# Füge OG Gruppen hinzu
try
{
$Context.LogMessage("Die Gruppe: " + $group.Get("name") + " wird dem Benutzer hinzugefügt.", "Information")
$group.Add($Context.TargetObject.AdsPath)
}
catch
{
$Context.LogMessage($group.Get("name") + ": " + $_.Exception.Message, "Warning")
}
} elseif ($group.Get("name") -like "AG *" -or $group.Get("name") -like "AG_*") {
# Füge AG Gruppen hinzu. # Business Rule triggert Approval
try
{
$Context.LogMessage("Die Gruppe: " + $group.Get("name") + " wird dem Benutzer hinzugefügt.", "Information")
$group.Add($Context.TargetObject.AdsPath)
}
catch
{
$Context.LogMessage($group.Get("name") + ": " + $_.Exception.Message, "Warning")
}
} elseif ($group.Get("name") -like "SG *" -or $group.Get("name") -like "SG_*") {
# Skippe SG Gruppen wegen Sonderberechtigungen
$Context.LogMessage("Skipping: " + $group.Get("name") + " wegen SG...", "Information")
} else {
# Alle anderen Gruppen sind nicht nach Standard. Ignorieren.
$Context.LogMessage("Skipping: " + $group.Get("name") + " - non default...", "Information")
}
}