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 !