Search code examples
azurepowershellazure-powershell

Azure PowerShell: New-AzRoleAssignment doesn't work with ApplicationId instead of ObjectId


despite azure documentation I don;t think you can use New-AzRoleAssignment with ApplicationId inplace of of ObjectId

docs: https://learn.microsoft.com/en-us/powershell/module/az.resources/new-azroleassignment?view=azps-13.0.0#example-5

$appId = 'f9f6b23d-eff8-48e1-a7f9-fd636a32e62c' # random GUID - could be web/func app ID  
$roleName='Azure Service Bus Data Owner'

New-AzRoleAssignment -ApplicationId $appId -RoleDefinitionName $roleName

Getting error:

New-AzRoleAssignment: 'PrincipalId' cannot be null.

Can anyone tell me what I'm missing?


Solution

  • I got the same error message like you:

    enter image description here

    To resolve the error, you need to give application_id for your Service Principal means Application_id of that Enterprise Application.

    enter image description here

    I ran same powershell command:

    $appId = '<application_id of your Service Principal> '
    $roleName='Azure Service Bus Data Owner'
    
    New-AzRoleAssignment -ApplicationId $appId -RoleDefinitionName $roleName
    

    Response:

    enter image description here

    I verified it from portal as well:

    enter image description here

    Reference:

    Application and service principal objects in Microsoft Entra ID

    New-AzRoleAssignment