Peter Eccles
Peter Eccles

Reputation: 1

Adding group members via Azure Graph API and Powershell

I have managed to connect to the Graph API and I'm able to pull data without any issues. I now want to add a user to a group and I cannot for the life of me get it to work. The MS documentation says its POST https://graph.microsoft.com/v1.0/groups/{id}/members/$ref. I believe $ref is the reference to the user in the format below. How, in Powershell, do I submit this using Invoke-RestMethod?

{
    "@odata.id":  "https://graph.microsoft.com/v1.0/users/a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx"
}  

Upvotes: 0

Views: 2904

Answers (1)

Jim Xu
Jim Xu

Reputation: 23141

According to my reserach, please try to update your body as

{
    "@odata.id":  "https://graph.microsoft.com/v1.0/directoryObjects/a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx"
}  

For example

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Bearer <access_token>")
$body = "{`"@odata.id`": `"https://graph.microsoft.com/v1.0/directoryObjects/<the user objectid>`"}"

$response = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups/022af724-22e4-4838-92e9-4e561f9acc0c/members/$ref' -Method 'POST' -Headers $headers -Body $body

Upvotes: 1

Related Questions