Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

AADUser Batch pulls AdministrativeUnits and process as groups

Open mpoulson opened this issue 6 months ago • 2 comments

Description of the issue

https://github.com/Microsoft365DSC/Microsoft365DSC/blob/Dev/Modules/Microsoft365DSC/DSCResources/MSFT_AADUser/MSFT_AADUser.psm1#L231

Batch MemberOf pulls Administrative Units which then breaks Remove-MgGroupMemberDirectoryObjectByRef because the Get-mgroup returns null.

Fix is

  1. update Batch to url = "/users/$($UserPrincipalName)/memberOf/microsoft.graph.group?`$select=displayName&`$filter=not(groupTypes/any(c:c eq 'DynamicMembership'))"
  2. add if null check for the else clause before Remove-MgGroupMemberDirectoryObjectByRef
 if ($null -eq $group)
{
    New-M365DSCLogEntry -Message 'Error updating data:' `
        -Exception "Attempting to add a user to a group that doesn't exist" `
        -Source $($MyInvocation.MyCommand.Source) `
        -TenantId $TenantId `
        -Credential $Credential

    throw "Group '$($_.InputObject)' does not exist in tenant"
}

Microsoft 365 DSC Version

Dev

Which workloads are affected

Azure Active Directory (Entra ID)

The DSC configuration


Verbose logs showing the problem

Cannot bind argument to parameter 'GroupId' because it is an empty string.                                                  + CategoryInfo          : InvalidData: (:) [], CimException                                                             + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Remove-MgGroupMemberDirectoryObjec     tByRef                                                                                                                   + PSComputerName        : localhost

Environment Information + PowerShell Version


mpoulson avatar Jun 04 '26 01:06 mpoulson

I don't have time to make cr, so if you could please

mpoulson avatar Jun 04 '26 01:06 mpoulson

Thanks for raising this @mpoulson confirmed the root cause and have a slightly refined version of the fix.

Root Cause

The batch request hits /memberOf which returns all directory objects including Administrative Units. When Get-MgGroup is then called with an Administrative Unit ID it returns $null, causing Remove-MgGroupMemberDirectoryObjectByRef to receive an empty GroupId.

Proposed Fix

1. Change the batch URL in Get-TargetResource to cast to groups only and filter client-side:

@{
    id      = 'MemberOf'
    method  = 'GET'
    url     = "/users/$($UserPrincipalName)/memberOf/microsoft.graph.group?`$select=displayName,groupTypes"
    headers = @{
        'ConsistencyLevel' = 'eventual'
    }
}

Then update the $currentMemberOf line below it to filter dynamic groups client-side:

[array]$currentMemberOf = ($batchResponse | Where-Object -FilterScript { $_.id -eq 'MemberOf' }).body.value |
    Where-Object { $_.groupTypes -notcontains 'DynamicMembership' } |
    Select-Object -ExpandProperty DisplayName

Note: The /microsoft.graph.group OData cast means Administrative Units are excluded at the API level. The $filter=not(groupTypes/any(...)) was removed from the URL because combining it with an OData cast in a batch request can cause a 400 Bad Request so the dynamic group filtering is moved client-side instead.

2. Add a null guard in Set-TargetResource before Remove-MgGroupMemberDirectoryObjectByRef:

else
{
    # Group that user is a member of is not present in MemberOf, remove user from group
    if ($null -ne $group)
    {
        Remove-MgGroupMemberDirectoryObjectByRef -GroupId $group.Id -DirectoryObjectId $userId
    }
    else
    {
        Write-Verbose -Message "Skipping removal of '$($_.InputObject)': not found as a group object (may be an Administrative Unit)."
        New-M365DSCLogEntry -Message 'Error updating data:' `
            -Exception "Skipping non-group object during member removal: '$($_.InputObject)'" `
            -Source $($MyInvocation.MyCommand.Source) `
            -TenantId $TenantId `
            -Credential $Credential
    }
}

Note: Using if ($null -ne $group) rather than throw here throwing would abort the entire ForEach-Object loop and stop processing remaining group changes. Logging and skipping is the safer behaviour.

Happy to open a PR if that helps.

saeekailas avatar Jun 04 '26 02:06 saeekailas