The Id
value you are retrieving via the Get-MgGroupMember cmdlet is not the same thing as the value the Get-MgDeviceManagementManagedDevice cmdlet expects. You cannot pass this value, as you are doing in your example. Moreover, the Get-MgDeviceManagementManagedDevice cmdlet will only accept the device Id from Intune, not the one returned via Entra ID (i.e. the Get-MgGroupMember cmdlet).
Instead, what you need to do is filter by the device's Entra ID identifier, which you can do as follows:
Get-MgDeviceManagementManagedDevice -Filter "azureADDeviceId eq '05ab7c00-ea9d-4c1b-8dc2-ef539bf2a27b'"
Adapted to your example, it will looks something like this:
$deviceCompliance = $deviceMembers | ForEach-Object {
$device = Get-MgDeviceManagementManagedDevice -Filter "azureADDeviceId eq `'$($_.DeviceId)`'"
[PSCustomObject]@{
DeviceName = $device.deviceName
ComplianceState = $device.complianceState
}
}