Forum Discussion
How to grant permissions on behalf of the organization Script
Try this:
Create the Service Principal and Assign Permissions:
# Define the app registration details
$appName = "YourAppName"
$app = New-MgApplication -DisplayName $appName
# Create a service principal for the app
$sp = New-MgServicePrincipal -AppId $app.AppId
# Define the permissions you need
$permissions = @(
"User.Read",
"Group.ReadWrite.All"
)
# Assign the permissions to the service principal
foreach ($perm in $permissions) {
$apiPermission = Get-MgServicePrincipalOauth2PermissionGrant -Filter "scope eq '$perm'"
New-MgServicePrincipalOauth2PermissionGrant -ClientId $sp.Id -ConsentType "AllPrincipals" -PrincipalId $null -ResourceId $apiPermission.ResourceId -Scope $perm
}
Grant Admin Consent:
# Grant admin consent for the permissions
$consent = New-MgServicePrincipalOauth2PermissionGrant -ClientId $sp.Id -ConsentType "AllPrincipals" -PrincipalId $null -ResourceId $app.AppId -Scope ( $permissions -join " " )