I am trying to grant an app role to a newly created service as part of my bicep script, but I cannot seem to get the MS Graph module loaded/installed.
This is the script:
resource roleAssignment 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'assign-custom-role'
location: location
kind: 'AzurePowerShell'
properties: {
azPowerShellVersion: '10.4'
retentionInterval: 'P1D'
arguments: '-managedIdentityObjectId ${containerApp.identity.principalId}'
scriptContent: '''
param (
[string] $managedIdentityObjectId
)
Install-Module Microsoft.Graph -Scope CurrentUser
$tenantID = 'xxxx'
Connect-AzureMG -TenantId $tenantID -Scopes 'Application.Read.All'
# The name of the server app that exposes the app role.
$serverApplicationName = 'My API'
# The name of the app role that the managed identity should be assigned to.
$appRoleName = 'MyApi.FullAccess'
# Look up the details about the server app's service principal and app role.
$serverServicePrincipal = (Get-MGServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
$serverServicePrincipalObjectId = $serverServicePrincipal.Id
$appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
# Assign the managed identity access to the app role.
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $managedIdentityObjectId `
-AppRoleId $appRoleId `
-PrincipalId $managedIdentityObjectId `
-ResourceId $serverServicePrincipalObjectId
'''
}
}
This throws an exception when executed both locally and by Azure DevOps Pipelines: "The term 'Connect-AzureMG' is not recognized as a name of a cmdlet, function, script file, or executable program"
What am I doing wrong?
Initially, I got the same error by using same script as you:
To resolve the error, modify the script by using
Connect-MgGraph
like below:I modified the script and executed successfully like below via PowerShell:
The App role assigned to the Managed Identity successfully:
Reference:
New-MgServicePrincipalAppRoleAssignment (Microsoft.Graph.Applications) | Microsoft