bashazureazure-cli

Fetch a service principle client ID having only its display name


The following bash script code used to work to get a service principal's client ID via AZ CLI, by passing its display name to the script.

az ad sp show --id http://$SP_NAME

But now I am getting the following error :

Service principal ... doesn't exist

The $SP_NAME variable contains the exact same display name as I see in the App Registrations in Azure AD.


When deploying a service principal via script, we do not have the service principal object ID prior to running the script, only the name we want to assign to the service principal (display name). Not only this used to work a few months back, as per the documentation it should still work!


Any idea how can I make it work again?


Solution

  • When executing the command az ad sp show --id http://$SP_NAME it tries to resolve the service principal by searching the exact id in the ServicePrincipalNames element of the AD Application object, as shown here on the az cli Github.

    When you create a service principal, be sure to add http://$SP_NAME as an application identifier URI, like below. app registration overview

    Then if you go to the Manifest of that App Registration, the Application ID URI is shown under identifierUris. identifier URIs

    The ServicePrincipalNames element is a collection of identifiers. So now the command should output the correct service principal. output