Search code examples
azureazure-active-directoryazure-resource-managerazure-bicep

Azure bicep get application id of service principal


Bicep can be used to create a role assignment as follows:

resource RoleAssignment 'Microsoft.Authorization/roleAssignments@2020-10-01-preview' = {
  name: guid(managementGroup().id, RoleDefinitionId, principalId)
  properties: {
    roleDefinitionId: roleDefinition.id
    principalId: principalId
    principalType: principalType
  }
}

Where the principal type is 'ServicePrincipal', it seems the application id from the Enterprise Application page of the Azure portal is required:

enter image description here

Does anyone know how to acquire this programatically? If it's not possible using bicep then perhaps PowerShell?


Solution

  • To get the Application ID of service principal via PowerShell, you can make use of below command:

    (Get-AzADServicePrincipal -DisplayName AppName).AppId
    

    I tried to reproduce the same in my environment and got below results:

    I have one service principal named ClientApp and got the Application ID of it successfully as below:

    (Get-AzADServicePrincipal -DisplayName ClientApp).AppId
    

    Response:

    enter image description here