How to fetch Object ID from Device Name

IntuneUser 181 Reputation points
2023-08-25T03:50:06.98+00:00

I would like to have an API where I put in the Device Name and fetch the Object ID and AAD ID of the device.

As per article: https://learn.microsoft.com/en-us/graph/api/device-get?view=graph-rest-1.0&tabs=http, the query is being run using object ID.

I tried to use API: https://graph.microsoft.com/v1.0/devices/?$filter(displayName eq 'DESKTOP-RRNFP6H')&$select=id,deviceId,displayName however it returned me all the device including the specified device.User's image

Request some help on this please.

Thanks.

Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Intune Other
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Nick Eckermann 606 Reputation points
    2023-08-25T13:20:02.54+00:00

1 additional answer

Sort by: Most helpful
  1. Deepanshukatara-6769 16,565 Reputation points Moderator
    2023-08-25T05:35:50.01+00:00

    Hope you are having a great day!

    The $filter parameter might not be supported for the displayName field in the Microsoft Graph API, which could be why you're getting all devices instead of just the specified one. In this case, you might need to retrieve all devices and then filter the results on your end based on the displayName you're looking for. You can use device ID parameter.

    So , The following request retrieves the id and extensionAttributes property of a device.

    Example--> https://graph.microsoft.com/beta/devices/6a59ea83-02bd-468f-a40b-f2c3d1821983?$select=id,extensionAttributes

    Moreover, if you need to do using deviceName/displayName , you can use below script to achieve this .

    $deviceName = "DESKTOP-RRNFP6H"
    $uri = "https://graph.microsoft.com/v1.0/devices?$select=id,deviceID,displayName"
    
    $accessToken = "YOUR_ACCESS_TOKEN_HERE"  # Replace with your actual access token
    
    $headers = @{
        "Authorization" = "Bearer $accessToken"
    }
    
    $response = Invoke-Rest Method -Uri $uri -Headers $headers -Method Get
    
    if ($response.StatusCode -eq 200) {
        $devices = $response.value
        
        for each ($device in $devices) {
            if ($device.displayName -eq $deviceName) {
                $deviceId = $device.id
                $aadId = $device.deviceID
                Write-Host "Device Name: $deviceName"
                Write-Host "Object ID: $deviceId"
                Write-Host "AAD ID: $aadId"
                break
            }
        }
    } else {
        Write-Host "Error: $($response.StatusCode) - $($response.Content)"
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.