Hi all,
I got a script that works, but is kinda finicky:
$memberListProperty = "adm-CustomAttributeTextMultiValue1"
try
{
$records = $Context.TargetObject.GetEx($memberListProperty)
}
catch
{
$records = $null
}
$guids = @()
for ($i = 0; $i -lt $records.Length; $i++)
{
$record = $records[$i]
$guid = [regex]::Match(($record | Select-String -Pattern "Adaxes\:\/\/<GUID=.+\>").Matches[0].Value, '(?<=GUID=)[^>]+').Value
$guids += $guid
}
$groupname = $context.TargetObject.Get("distinguishedName")
if ($groupname -like "*onmicrosoft*"){
$servername = "fraisalan.onmicrosoft.com"
} else {
$servername = "fraisalan.ch"
}
$members = Get-AdmGroupMember -Identity $groupname -AdaxesService localhost -Server $servername
foreach ($member in $members)
{
#Get ObjectGUID of Member
$binded = $Context.BindToObjectByDN($member)
[Guid]$nguid = $binded.Get("ObjectGUID")
#Check if it is in the array
if ($guids -contains $nguid){
$Context.LogMessage("$member wurde nicht gelöscht, da es über den custom command zeitlich befristet wurde.","Information")
} elseif ($member -notlike "*GG_RBA_AD-Bellach*"){
$Context.LogMessage("$member wurde gelöscht.", "Information")
Remove-AdmgroupMember -Identity $groupname -Members $member -AdaxesService localhost -Confirm:$false -Server $servername
} else{
$Context.LogMessage("$member wurde nicht gelöscht, da es sich um eine Gruppe handelt.","Information")
}
}
The script runs for groups, and it should check if a member was added through a custom comand, or manually to the group. If he was added manually, then we will delete him.
My big problem is that the group can exist both on-prem and in the cloud.
The Adaxes Powershell functions (like Get-AdmGroupMember, or Remove-AdmgroupMember) only work with cloud objects if the server name is specified, otherwise they fail.
How can I build it nicer so that I don't have to hardcode the server name?
So basically I want to replace this part:
$groupname = $context.TargetObject.Get("distinguishedName")
if ($groupname -like "*onmicrosoft*"){
$servername = "fraisalan.onmicrosoft.com"
} else {
$servername = "fraisalan.ch"
}
Thank you for your help!
Best regards
Benjamin