I’m trying to write a script to update the permissions for a managed identity using Powershell and the REST API, but I’m running into a problem and I don’t know how to troubleshoot it any further.
My script connects to an App Registration with App ID and Secret, generates a token and I use that token in my "Invoke-RestMethod" command.
Connecting works fine, and I can run GET commands against the endpoints, but trying to update it using a POST gives me an error of "Not a valid reference update".
This is the ‘bit of code:
# Payload in Hashtable
$POSTBody = @{
principalID = "$($MIDData.id)"
resourceID = "$ObjectID"
appRoleId = "$($TEMProle.id)"
}
# check if Hashtable converts to JSON
$POSTBody | convertto-json
$AppRoleAssignment = "https://graph.microsoft.com/beta/servicePrincipals/$ObjectID/appRoleAssignedTo"
Invoke-RestMethod -Uri $AppRoleAssignment -Headers @{Authorization = "Bearer $($TokenAccess)" } -Method POST -Body $($POSTBody | convertto-json) -ContentType "application/json"
I’m confident my variables are correct. I’ve tried hard-coding them as well, but I get the same result.
Any wisdom or guidance would be most appreciated.
2
Answers
Note that, you need to pass Object ID of Microsoft Graph Enterprise Application for
resourceID
parameter.In my case, I ran below API call to get the object ID of Microsoft Graph service principal:
Now I ran below PowerShell script to generate token and call API to add
User.Read.All
permission of Application type to managed identity:Response:
To confirm that, I checked the same in Azure Portal where
User.Read.All
permission of Application type added successfully to managed identity like this:Reference:
Grant an appRoleAssignment for a service principal – Microsoft Graph
Need attention to the id you passed in.
Object ID
of the service principal, not theApplication ID
permission Id
, It is related to the resourceId. If you set microsoft graphGroup.Read.All
, the example id is5b567255-7703-4780-807c-7be8301ae99b
, just check hereObject id
of the resource. If the resource isMicrosoft Graph
, which have the Application ID00000003-0000-0000-c000-000000000000
, and theObject ID
3b7856dd-b583-4c67-aea4-fe8ec1af1dd0
, Just use theObject Id
, not theApplication ID
I have a example in postman, it is the same as script. Noting my passing Ids.