Cannot delete devices from the Intune using PS.
To get devices I use Graph API (https://learn.microsoft.com/en-us/graph/api/intune-devices-manageddevice-list?view=graph-rest-1.0&tabs=http).
To delete I use https://learn.microsoft.com/en-us/graph/api/intune-devices-manageddevice-delete?view=graph-rest-1.0&tabs=http. The GrpahAPI expects parameter deviceId.
E.g.:
Invoke-MgGraphRequest -Method DELETE -Uri "v1.0/deviceManagement/managedDevices/$($ManagedDevice.id)"
Or
Remove-MgDeviceManagementManagedDevice -ManagedDeviceId $ManagedDevice.id
If I run any of these commands, they complete without errors but do not delete the device.
I tried to change one digit in device id and commands returned the error. So endpoint parses the id, not ignores it.
I analyzed what happens when I delete the device from the UI.
The system sends: DELETE to https://graph.microsoft.com/v1.0/devices/%Device_Object_Id%
And this is the problem. Object Id is visible only from the UI and not returned when I use the code from the first link.
E.g.
The device from the UI:
When I load same device using PS, I get Device ID, but do not get Object ID.
If I delete this device from the UI, the system will send DELETE to https://graph.microsoft.com/v1.0/devices/aaaaaaaa-aaaa-aaaaa-8f83-fc0c3444cf31
If I run
Remove-MgDeviceManagementManagedDevice -ManagedDeviceId $DeviceID
nothing happens. No errors, and the device is not deleted.
If I delete the device from UI it uses Object Id:
Is this expected behavior or a bug? If the behavior is expected where I can find the object id?