Assign Member to Group via Powershell Graph API Call throws Request_BadRequest
-
08-02-2021 - |
Question
Calling the following code:
$uri = "https://graph.microsoft.com/v1.0/groups/[groupID]/members/`$ref"
$body = (@{'@odata.id'='https://graph.microsoft.com/v1.0/directoryObjects/[memberID]'} | ConvertTo-JSON)
Invoke-WebRequest -Method "POST" -Uri $uri -ContentType "application/json" -Headers @{Authorization = "Bearer $retrievedToken"} -Body $body -UseBasicParsing -Verbose
Returns:
Invoke-WebRequest : { "error": { "code": "Request_BadRequest", "message": "No URI value was found for
an entity reference link. A single URI value was expected."
Obs.: The same request in Graph Explorer works as expected.
Solution
Worked with:
$uri = "https://graph.microsoft.com/v1.0/groups/[groupID]/members/`$ref"
$body = (@{'@odata.id'='https://graph.microsoft.com/v1.0/directoryObjects/[MemberID]'} | ConvertTo-JSON)
Invoke-WebRequest -Method "POST" -Uri $uri -ContentType "application/json" -Headers @{Authorization = "Bearer $token"} -Body $body
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange