添加组成员批量请求

问题描述 投票:0回答:1

我正在尝试创建批处理请求以将用户添加为多个O365组的成员。我正在尝试使用powershell创建JSON。并使用PSMSGraph模块提交请求。

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 ++
    }      

我正在使用以下循环将请求添加到数组中。填充数组后,我将其转换为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"
                 }
    ]
}

发布请求时,我最终出现以下错误:

Invoke-GraphRequest:无法查询Uri'https://graph.microsoft.com/v1.0/ $ batch':远程服务器返回错误:(400)错误请求:{“error”:{“code”:“BadRequest”,“message”:“写入请求ID :0不包含Content-Type标题或正文。“,”innerError“:{”request-id“:”fdd0362b-c850-4f9f-b1a8-0020f60a1801“,”date“:”2019-02-21T14:51: 06“}}}

最有可能是身体畸形。任何想法如何为BATCH请求创建正确格式的正文?

谢谢 !

powershell azure-active-directory microsoft-graph office365api
1个回答
0
投票

不需要使用这种方式,有一个内置的powershell命令可以将一个成员添加到一个组中,该命令基本上调用了azure ad graph api。

见: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"

如果要将用户添加到多个组,只需使用循环即可。

© www.soinside.com 2019 - 2024. All rights reserved.