Search code examples
azure-active-directoryazure-powershellazure-cli

How to Get Azure AD Object by Object ID Using Azure CLI


In the Azure Portal, one can look-up an Azure AD object based on the Object ID as shown below:

enter image description here

Is it possible to retrieve an Azure AD object by the Object ID using the Azure CLI?

In order to use the Azure CLI to get the object related to the object ID, it appears that I need to know in advance if the related resource is a user, group, device, app registration, etc., in order to get the details. For example, if I know the Object ID is a user, I can use az ad user show --id. If all I have is the Object ID, I don't know the 'type' of the object, yet somehow the Portal can figure this out!

While I'd prefer an Azure CLI solution, an Azure PowerShell solution would be better than nothing. I am asking the question because I'm trying to generate a list of access policies within key vault using az keyvault list, but the access policy list from that CLI command just shows Object IDs for each policy... I have no way of determining if the objects are users, groups, etc.

enter image description here


Solution

  • If you want to get Azure AD resource with its object id, we can use the following Microsoft Graph API

    POST https://graph.microsoft.com/v1.0/directoryObjects/getByIds
    Content-type: application/json
    
    {
        "ids":[""]
    }
    

    If you want to call the Microsoft Graph with Azure CLI, we can use the command az rest

    For example (I use Azure cloud shell)

    az rest --method POST --url 'https://graph.microsoft.com/v1.0/directoryObjects/getByIds' --headers 'Content-Type=application/json'  --body '{"ids":[""]}'
    

    enter image description here

    For more details, please refer to here nad here