Skip to content

Commit 3d6f799

Browse files
Merge pull request #198152 from johndowns/app-role-powershell-graph
Update managed identity role assignment article to use Microsft Graph SDK
2 parents 1b822a6 + 7057302 commit 3d6f799

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

articles/active-directory/managed-identities-azure-resources/how-to-assign-app-role-managed-identity-powershell.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.subservice: msi
1212
ms.topic: how-to
1313
ms.tgt_pltfrm: na
1414
ms.workload: identity
15-
ms.date: 12/10/2020
15+
ms.date: 05/12/2022
1616
ms.author: jodowns
1717
ms.collection: M365-identity-device-management
1818
ms.custom: devx-track-azurepowershell
@@ -27,15 +27,13 @@ Managed identities for Azure resources provide Azure services with an identity i
2727
2828
In this article, you learn how to assign a managed identity to an application role exposed by another application using Azure AD PowerShell.
2929

30-
[!INCLUDE [az-powershell-update](../../../includes/updated-for-az.md)]
31-
3230
## Prerequisites
3331

3432
- If you're unfamiliar with managed identities for Azure resources, check out the [overview section](overview.md). **Be sure to review the [difference between a system-assigned and user-assigned managed identity](overview.md#managed-identity-types)**.
3533
- If you don't already have an Azure account, [sign up for a free account](https://azure.microsoft.com/free/) before continuing.
3634
- To run the example scripts, you have two options:
3735
- Use the [Azure Cloud Shell](../../cloud-shell/overview.md), which you can open using the **Try It** button on the top-right corner of code blocks.
38-
- Run scripts locally by installing the latest version of [Azure AD PowerShell](/powershell/azure/active-directory/install-adv2).
36+
- Run scripts locally by installing the latest version of [the Az PowerShell module](/powershell/azure/install-az-ps) and the [Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/get-started).
3937

4038
## Assign a managed identity access to another application's app role
4139

@@ -62,7 +60,7 @@ In this article, you learn how to assign a managed identity to an application ro
6260
1. Find the object ID of the service application's service principal. You can find this using the Azure portal. Go to Azure Active Directory and open the **Enterprise applications** page, then find the application and look for the **Object ID**. You can also find the service principal's object ID by its display name using the following PowerShell script:
6361
6462
```powershell
65-
$serverServicePrincipalObjectId = (Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationName'").ObjectId
63+
$serverServicePrincipalObjectId = (Get-MgServicePrincipal -Filter "DisplayName eq '$applicationName'").Id
6664
```
6765
6866
> [!NOTE]
@@ -88,19 +86,23 @@ In this article, you learn how to assign a managed identity to an application ro
8886
* `serverServicePrincipalObjectId`: the object ID of the server application's service principal, which you found in step 4.
8987
* `appRoleId`: the ID of the app role exposed by the server app, which you generated in step 5 - in the example, the app role ID is `0566419e-bb95-4d9d-a4f8-ed9a0f147fa6`.
9088
91-
Execute the following PowerShell script to add the role assignment:
89+
Execute the following PowerShell command to add the role assignment:
9290
9391
```powershell
94-
New-AzureADServiceAppRoleAssignment -ObjectId $managedIdentityObjectId -Id $appRoleId -PrincipalId $managedIdentityObjectId -ResourceId $serverServicePrincipalObjectId
92+
New-MgServicePrincipalAppRoleAssignment `
93+
-ServicePrincipalId $managedIdentityObjectId `
94+
-PrincipalId $managedIdentityObjectId `
95+
-ResourceId $serverServicePrincipalObjectId `
96+
-AppRoleId $appRoleId
9597
```
9698
9799
## Complete script
98100
99101
This example script shows how to assign an Azure web app's managed identity to an app role.
100102
101103
```powershell
102-
# Install the module. (You need admin on the machine.)
103-
# Install-Module AzureAD
104+
# Install the module.
105+
# Install-Module Microsoft.Graph -Scope CurrentUser
104106
105107
# Your tenant ID (in the Azure portal, under Azure Active Directory > Overview).
106108
$tenantID = '<tenant-id>'
@@ -118,19 +120,19 @@ $appRoleName = '<app-role-name>' # For example, MyApi.Read.All
118120
# Look up the web app's managed identity's object ID.
119121
$managedIdentityObjectId = (Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $webAppName).identity.principalid
120122
121-
Connect-AzureAD -TenantId $tenantID
123+
Connect-MgGraph -TenantId $tenantId -Scopes 'Application.Read.All','Application.ReadWrite.All','AppRoleAssignment.ReadWrite.All','Directory.AccessAsUser.All','Directory.Read.All','Directory.ReadWrite.All'
122124
123125
# Look up the details about the server app's service principal and app role.
124-
$serverServicePrincipal = (Get-AzureADServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
126+
$serverServicePrincipal = (Get-MgServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
125127
$serverServicePrincipalObjectId = $serverServicePrincipal.ObjectId
126128
$appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
127129
128130
# Assign the managed identity access to the app role.
129-
New-AzureADServiceAppRoleAssignment `
130-
-ObjectId $managedIdentityObjectId `
131-
-Id $appRoleId `
131+
New-MgServicePrincipalAppRoleAssignment `
132+
-ServicePrincipalId $managedIdentityObjectId `
132133
-PrincipalId $managedIdentityObjectId `
133-
-ResourceId $serverServicePrincipalObjectId
134+
-ResourceId $serverServicePrincipalObjectId `
135+
-AppRoleId $appRoleId
134136
```
135137

136138
## Next steps

0 commit comments

Comments
 (0)