0

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"
}  
4
  • Maybe something along the lines of stackoverflow.com/questions/35722865/… for example? Commented Mar 9, 2020 at 21:08
  • If powershell is your language of choice, why not using the AzureAD module? Commented Mar 9, 2020 at 21:14
  • @sodawillow yeah, something like that, but I think I'm missing something. Posting the data above as the body isn't working. Commented Mar 9, 2020 at 21:20
  • @ManuelBatsching I have managed to do everything else I needed to do using the Graph API...I'd like to understand how to do this too. Commented Mar 9, 2020 at 21:21

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank Jim Xu, that was just the nudge in the right dirction I needed. I was almost there like I thought. Here is a snippet of my working code.
$group_id = "8a1e0604-b102-48c2-a32a-c82a026b7f23" $user_id = "a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx" $body = [PSCustomObject]@{"@odata.id"= "graph.microsoft.com/v1.0/users/$($user_id)"} | ConvertTo-Json ### add user to group ### Invoke-RestMethod -UseBasicParsing -Headers $HeaderParams -Uri "graph.microsoft.com/v1.0/groups/$($group_id)`$ref" -Method Post -ContentType application/json -Body $body
$group_id = "8a1e0604-b102-48c2-a32a-c82a026b7f23" $user_id = "a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx" $body = [PSCustomObject]@{"@odata.id"= "graph.microsoft.com/v1.0/users/$($user_id)"} | ConvertTo-Json ### remove user from group ### Invoke-RestMethod -UseBasicParsing -Headers $HeaderParams -Uri "graph.microsoft.com/v1.0/groups/$($group_id)/members/…`$ref" -Method Delete -ContentType application/json -Body $body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.