通过Azure Graph API和Powershell添加组成员

时间:2020-03-09 20:54:55

标签: powershell azure-ad-graph-api

我设法连接到Graph API,并且能够提取数据而没有任何问题。现在,我想将用户添加到组中,但我一生都无法正常工作。 MS文档说其POST https://graph.microsoft.com/v1.0/groups/{id}/members/$ref。我相信$ref是下面格式的对用户的引用。如何在Powershell中使用Invoke-RestMethod提交此内容?

{
    "@odata.id":  "https://graph.microsoft.com/v1.0/users/a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx"
}  

1 个答案:

答案 0 :(得分:1)

根据我的研究,请尝试将您的身体更新为

{
    "@odata.id":  "https://graph.microsoft.com/v1.0/directoryObjects/a0fbxxxb7-2b3d-4df1-a0ce-3bfdb513dxxx"
}  

例如

$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