Add a role to an application to a resource group via PowerShell script

153 Views Asked by At

I have a Resource Group 'test-rg'. How can I add a custom role 'Custom Role' to an application 'test-application' to this resource group via PowerShell script?

enter image description here

1

There are 1 best solutions below

0
Vlad DX On BEST ANSWER

I believe by

"How can I add a custom role 'Custom Role' to an application 'test-application' to this resource group via PowerShell script?"

you actually mean:

"How can I create a role assignment of the role 'Custom Role' for the application 'test-application' for a Resource Group by PowerShell?"


If that's the case, you need to just create a role assignment.

https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-powershell

Assign Azure roles using Azure PowerShell - Azure RBAC | Microsoft Learn

To do it in PowerShell, you need to run a following command:

New-AzRoleAssignment -ObjectId <objectId> `
-RoleDefinitionName <roleName> `
-Scope /subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>

Where:

  • <objectId> is an Object ID of your Service Principal / Enterprise Application,
  • <roleName> is a built-in or a custom role (e.g., Custom Role),
  • <subscriptionId> and <resourceGroupName> are identifier of respective Subscription and Resource Group you want to give access to.

Example:

New-AzRoleAssignment -ObjectId "ae75b865-abf1-4376-afb8-54ebbf0b2051" `
-RoleDefinitionName "Custom Role" `
-Scope /subscriptions/4270e84b-c064-450b-9c67-a4a449d319df/resourcegroups/test-rg