Here's how we store group information for offboarded users from our hybrid environment. When executed on an on-prem user, it creates a JSON representation of all of the on-prem and Entra groups a user is a member of. We then store that JSON in the offboarding ticket. If we ever had to recreate it, it's as simple as calling ConvertFrom-Json
on the data and iterating over each DN.
edited to include the Entra dynamic group check from the script support linked - I didn't think about that when I wrote mine. thanks!
$fullname = "%fullname%"
# get the guids of groups the user is a member of
try {
$groupGuidsBytes = $Context.TargetObject.GetEx("adm-MemberOfGuid")
}
catch {
$Context.LogMessage("Failed to retrieve group information for $fullname ", "Error")
$Context.LogException($_.Exception)
break
}
$entraGroups = @()
$onPremGroups = @()
$allGroups = @{}
foreach ($guidBytes in $groupGuidsBytes) {
try {
$guid = [guid]$guidBytes
$group = $Context.BindToObject("Adaxes://<GUID=$guid>")
$groupDn = $group.Get("distinguishedName")
if ($groupDn -like "*DC=onmicrosoft,DC=com") {
try {
# the $group.Get() method will throw an exception if the property isn't found
$group.Get("adm-AzureDynamicMembership")
$Context.LogMessage("Skipping Entra dynamic group $groupDn", "Information")
}
catch {
# if we're here, it means that it's not a dynamic group, so add it to the list
$entraGroups += $groupDn
}
}
else {
$onPremGroups += $groupDn
}
}
catch {
$Context.LogException($_.Exception)
}
}
$allGroups = @{
entra = $entraGroups
onprem = $onPremGroups
}
$groupJson = $allGroups | ConvertTo-Json -Compress
# output looks like this (didn't use the -Compress parameter here for readability). store the value of $groupJson wherever you'd like - we write it to our offboarding ticket
# {
# "entra": [
# "CN=group1\\0AUID:a76241feaeca41398697c968b805fb7e,OU=Groups,DC=contoso,DC=onmicrosoft,DC=com",
# "CN=group2\\0AUID:a76241feaeca41398697c968b805fb7e,OU=Groups,DC=contoso,DC=onmicrosoft,DC=com"
# ],
# "onprem": [
# "CN=Domain Users,CN=Users,DC=contoso,DC=com",
# "CN=Finance Users,CN=ContosoGroups,DC=contoso,DC=com"
# ]
# }