0
votes

I'm trying to create a Batch request to add user as member of multiple O365 groups. I'm trying to create the JSON using powershell. And submit the request using the PSMSGraph module.

foreach($AGM in $GraphUser.AddGuestMember){
    $myRequest = [pscustomobject][ordered]@{ 
        id     = $requestID
        method = "POST"
        url    = "/groups/$AGM/members/`$ref"
        body   = "@odata.idhttps://graph.microsoft.com/v1.0/users/$($GraphUser.GId)"
        }
    $myBatchRequests += $myRequest
    $IDs += $requestID
    $requestID ++
    }      

I'm using the following Loop to Add requests into an array. After filling the array I'm converting it to JSON.

{
"requests":  [
                 {
                     "id":  0,
                     "method":  "POST",
                     "url":  "/groups/be03ed64-639a-4620-b8a4-a025df70d131/members/$ref",
                     "body":  "@odata.id:https://graph.microsoft.com/v1.0/users/c9fc90c3-8eaf-43f2-a27f-d8176e893635"
                 },
                 {
                     "id":  1,
                     "method":  "POST",
                     "url":  "/groups/58389709-0176-4da9-93c9-05eb797fc32a/members/$ref",
                     "body":  "@odata.id:https://graph.microsoft.com/v1.0/users/c9fc90c3-8eaf-43f2-a27f-d8176e893635"
                 }
    ]
}

When posting the request I endup with the following error:

Invoke-GraphRequest : Unable to query Uri 'https://graph.microsoft.com/v1.0/$batch': The remote server returned an error: (400) Bad Request.: { "error": { "code": "BadRequest", "message": "Write request id : 0 does not contain Content-Type header or body.", "innerError": { "request-id": "fdd0362b-c850-4f9f-b1a8-0020f60a1801", "date": "2019-02-21T14:51:06" } } }

Most possibly the body is malformed. Any ideas how can I create the body in the right format for a BATCH request ?

Thanks !

2

2 Answers

1
votes

Add "headers":{"Content-Type":"application/json"} in the requests

-1
votes

No need to use this way, there is a built-in powershell command to add a member to a group, the command essentially calls the azure ad graph api.

See: https://docs.microsoft.com/en-us/powershell/module/azuread/add-azureadgroupmember?view=azureadps-2.0

Add-AzureADGroupMember -ObjectId "62438306-7c37-4638-a72d-0ee8d9217680" -RefObjectId "0a1068c0-dbb6-4537-9db3-b48f3e31dd76"

If you want to add a user to multiple groups, just use a loop to do that.