-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Description
In our organization, we were using Azure AD Commandlets to add a service principal as an owner to the app registration and the same code was working for the past 2 years but now it has started returning below error.
After migrating the script to use Microsoft Graph API Commandlets we are not able to add the service principal as an owner to other app registrations as it returns bad requests but the same command when we use it to add a user account works as expected.
Please let us know that, moving forward, Azure will not allow adding a service principal as an owner to app registration, or if there is a workaround for the above scenario.
Code Used in script:
if ("$($ownerAppDetails.Id)" -notin $existingOwners.Id) {
Write-Verbose -Verbose "Adding owner with client id "$($ownerAppDetails.AppId)" for application "$($appDetails.DisplayName)" with client id "$($appDetails.AppId)""
$id=$ownerServicePrincipal.Id
Write-Verbose -Verbose "id:$id"
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$id"
}
Write-Verbose -Verbose "NewOwner:$params"
$appRegobjectId = $appDetails.Id
Write-Verbose -Verbose "appRegAppId:$appRegobjectId"
# Add the owner application as an owner to the respective app registration
New-MgApplicationOwnerByRef -ApplicationId "$appRegobjectId" -BodyParameter $params
#Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/applications/$appRegobjectId/owners/\$ref" -Body ($params | ConvertTo-Json)
Write-Verbose -Verbose "Successfully added owner for application "$($appDetails.DisplayName)" with client id "$($appDetails.AppId)""
