azurepowershellazure-rbac

How do I add a role assignment in the IAM section of subscriptions using powershell? I'm looking to add "Reader" to a service principal


enter image description here

See my Azure Portal. I'm looking to add the "Reader" role my service principal. How would I do this in Powershell?


Solution

  • You can make use of below PowerShell command to add Reader role to the service principal under subscription:

    $spId = (Get-AzADServicePrincipal -DisplayName "sp_Name").id
    $subscriptionId = "your_subscription_Id"
    
    New-AzRoleAssignment -ObjectId $spId `
    -RoleDefinitionName "Reader" `
    -Scope "/subscriptions/$subscriptionId"
    

    Response:

    enter image description here

    To confirm that, I checked the same in Portal where Reader role added successfully to service principal under subscription level like this:

    enter image description here