0
votes

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"
}  
1
Maybe something along the lines of stackoverflow.com/questions/35722865/… for example? - sodawillow
If powershell is your language of choice, why not using the AzureAD module? - Manuel Batsching
@sodawillow yeah, something like that, but I think I'm missing something. Posting the data above as the body isn't working. - Peter Eccles
@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. - Peter Eccles

1 Answers

1
votes

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