Skip to content

Commit ad5590b

Browse files
Merge pull request #250513 from johndowns/aad-managed-identity-app-role-powershell-september-2023
Remove AzureAD PowerShell module reference
2 parents c69fb57 + af59e70 commit ad5590b

File tree

1 file changed

+5
-68
lines changed

1 file changed

+5
-68
lines changed

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

Lines changed: 5 additions & 68 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: 09/06/2023
15+
ms.date: 09/07/2023
1616
ms.author: jodowns
1717
ms.collection: M365-identity-device-management
1818
ms.custom: has-azure-ad-ps-ref
@@ -25,15 +25,15 @@ Managed identities for Azure resources provide Azure services with an identity i
2525
> [!NOTE]
2626
> The tokens that your application receives are cached by the underlying infrastructure, which means that any changes to the managed identity's roles can take significant time to take effect. For more information, see [Limitation of using managed identities for authorization](managed-identity-best-practice-recommendations.md#limitation-of-using-managed-identities-for-authorization).
2727
28-
In this article, you learn how to assign a managed identity to an application role exposed by another application using Azure AD PowerShell.
28+
In this article, you learn how to assign a managed identity to an application role exposed by another application using the Microsoft Graph PowerShell SDK.
2929

3030
## Prerequisites
3131

3232
- 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)**.
3333
- If you don't already have an Azure account, [sign up for a free account](https://azure.microsoft.com/free/) before continuing.
3434
- To run the example scripts, you have two options:
3535
- 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.
36-
- Run scripts locally by installing the latest version of [the Az PowerShell module](/powershell/azure/install-azure-powershell). You can also use the [Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/get-started).
36+
- Run scripts locally by installing the latest version of the [Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/get-started).
3737

3838
## Assign a managed identity access to another application's app role
3939

@@ -59,20 +59,10 @@ In this article, you learn how to assign a managed identity to an application ro
5959
6060
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:
6161
62-
# [Azure PowerShell](#tab/azurepowershell)
63-
64-
```powershell
65-
$serverServicePrincipalObjectId = (Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationName'").ObjectId
66-
```
67-
68-
# [Microsoft Graph](#tab/microsoftgraph)
69-
7062
```powershell
7163
$serverServicePrincipalObjectId = (Get-MgServicePrincipal -Filter "DisplayName eq '$applicationName'").Id
7264
```
7365
74-
---
75-
7666
> [!NOTE]
7767
> Display names for applications are not unique, so you should verify that you obtain the correct application's service principal.
7868
@@ -105,18 +95,6 @@ In this article, you learn how to assign a managed identity to an application ro
10595
10696
Execute the following PowerShell command to add the role assignment:
10797
108-
# [Azure PowerShell](#tab/azurepowershell)
109-
110-
```powershell
111-
New-AzureADServiceAppRoleAssignment `
112-
-ObjectId $serverServicePrincipalObjectId `
113-
-ResourceId $serverServicePrincipalObjectId `
114-
-Id $appRoleId `
115-
-PrincipalId $managedIdentityObjectId
116-
```
117-
118-
# [Microsoft Graph](#tab/microsoftgraph)
119-
12098
```powershell
12199
New-MgServicePrincipalAppRoleAssignment `
122100
-ServicePrincipalId $managedIdentityObjectId `
@@ -125,51 +103,10 @@ In this article, you learn how to assign a managed identity to an application ro
125103
-AppRoleId $appRoleId
126104
```
127105
128-
---
129-
130106
## Complete script
131107
132108
This example script shows how to assign an Azure web app's managed identity to an app role.
133109
134-
# [Azure PowerShell](#tab/azurepowershell)
135-
136-
```powershell
137-
# Install the module. This step requires you to be an administrator on your machine.
138-
# Install-Module AzureAD
139-
140-
# Your tenant ID (in the Azure portal, under Azure Active Directory > Overview).
141-
$tenantID = '<tenant-id>'
142-
143-
# The name of your web app, which has a managed identity that should be assigned to the server app's app role.
144-
$webAppName = '<web-app-name>'
145-
$resourceGroupName = '<resource-group-name-containing-web-app>'
146-
147-
# The name of the server app that exposes the app role.
148-
$serverApplicationName = '<server-application-name>' # For example, MyApi
149-
150-
# The name of the app role that the managed identity should be assigned to.
151-
$appRoleName = '<app-role-name>' # For example, MyApi.Read.All
152-
153-
# Look up the web app's managed identity's object ID.
154-
$managedIdentityObjectId = (Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $webAppName).identity.principalid
155-
156-
Connect-AzureAD -TenantId $tenantID
157-
158-
# Look up the details about the server app's service principal and app role.
159-
$serverServicePrincipal = (Get-AzureADServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
160-
$serverServicePrincipalObjectId = $serverServicePrincipal.ObjectId
161-
$appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
162-
163-
# Assign the managed identity access to the app role.
164-
New-AzureADServiceAppRoleAssignment `
165-
-ObjectId $serverServicePrincipalObjectId `
166-
-ResourceId $serverServicePrincipalObjectId `
167-
-Id $appRoleId `
168-
-PrincipalId $managedIdentityObjectId
169-
```
170-
171-
# [Microsoft Graph](#tab/microsoftgraph)
172-
173110
```powershell
174111
# Install the module.
175112
# Install-Module Microsoft.Graph -Scope CurrentUser
@@ -194,12 +131,12 @@ Connect-MgGraph -TenantId $tenantId -Scopes 'Application.Read.All','Application.
194131
195132
# Look up the details about the server app's service principal and app role.
196133
$serverServicePrincipal = (Get-MgServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
197-
$serverServicePrincipalObjectId = $serverServicePrincipal.ObjectId
134+
$serverServicePrincipalObjectId = $serverServicePrincipal.Id
198135
$appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
199136
200137
# Assign the managed identity access to the app role.
201138
New-MgServicePrincipalAppRoleAssignment `
202-
-ServicePrincipalId $managedIdentityObjectId `
139+
-ServicePrincipalId $serverServicePrincipalObjectId `
203140
-PrincipalId $managedIdentityObjectId `
204141
-ResourceId $serverServicePrincipalObjectId `
205142
-AppRoleId $appRoleId

0 commit comments

Comments
 (0)