Hello Allister,
Yes, it is possible. Use the below script.
$groupNamesToSkip = @("MyGroup1", "MyGroup2", "Department*") # TODO: modify me
function SkipGroup($patterns, $name)
{
foreach ($pattern in $patterns)
{
if ($name -like $pattern)
{
return $True
}
}
return $False
}
# Get all groups user is a direct member of
$groupGuids = $Context.TargetObject.GetEx("adm-DirectMemberOfGuid")
# Get the Primary Group ID
$primaryGroupId = $NULL
if ($Context.TargetObject.DirectoryType -eq 1)
{
$primaryGroupId = $Context.TargetObject.Get("primaryGroupID")
}
foreach ($groupGuidBytes in $groupGuids)
{
# Bind to the group
$groupGuid = New-Object "System.Guid" (,$groupGuidBytes)
$groupGuid = $groupGuid.ToString("B")
$groupPath = "Adaxes://<GUID=$groupGuid>"
$group = $Context.BindToObject($groupPath)
if ($group.DirectoryType -eq 1)
{
# Skip Primary Group
if ($group.Get("primaryGroupToken") -eq $primaryGroupId)
{
continue
}
$groupName = $group.Get("sAMAccountName")
}
else
{
continue
}
# Skip special groups
if (($groupNamesToSkip -ne $NULL) -and
(SkipGroup $groupNamesToSkip $groupName))
{
continue
}
# Remove user from the group
$group.Remove($Context.TargetObject.AdsPath)
}