I currently have code to create an application with Delegated permissions. I would like to add Application permissions as well. Can anyone give an example of how to add both? Thank you.
My code is below:
function Add-Permission {
param(
[string]$appName
)
$delegatedPermissions = @(
"AuditLog.Read.All",
"Directory.Read.All",
"User.Read.All",
"offline_access",
"Group.Read.All",
"GroupMember.Read.All",
"GroupMember.ReadWrite.All"
)
$filteredPermissions = Get-MgServicePrincipal -Filter "displayName eq 'Microsoft Graph'" `
-Property Oauth2PermissionScopes | Select-Object -ExpandProperty Oauth2PermissionScopes | `
Where-Object { $delegatedPermissions -contains $_.Value }
$azureServicePermission = @{
resourceAppId = "797f4846-ba00-4fd7-ba43-dac1f8f63013"
resourceAccess = @(
@{
id = "41094075-9dad-400e-a0bd-54e686782033"
type = "Scope"
}
)
}
$app = Get-MgApplication -Filter "DisplayName eq '$appName'"
$params = @{
requiredResourceAccess = @(
$azureServicePermission,
@{
resourceAppId = "00000003-0000-0000-c000-000000000000"
resourceAccess = $filteredPermissions | ForEach-Object {
@{
id = $_.Id
type = "Scope"
}
}
}
)
}
Update-MgApplication -ApplicationId $app.Id -BodyParameter $params
2
Answers
I would try to get the application permissions just like you are doing for the delegated permissions and then add that to the params.
Your function starts off well, but I’d use the
New-MgServicePrincipalAppRoleAssignedTo
cmdlet to add the app role assignments.https://learn.microsoft.com/en-us/graph/permissions-grant-via-msgraph?tabs=powershell&pivots=grant-application-permissions#step-2-grant-an-app-role-to-a-client-service-principal