Invoke-retMethod:远程服务器返回错误:(400)不良请求。 Azure AAD添加组成员
我遵循Azure文档,并创建了以下请求,使用PowerShell脚本将成员添加到AD组中。但是获得错误的不良请求。
$clientId = "d4b2ca57-yyyy-yyyy-zzzz-50a4ca126390"
$clientSecret = "nnr7Q~zzzzz"
$tenantID = "4f6eyyyy-yyyy-yyyy-yyyy-0981d022yyyy"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientId
Client_Secret = $clientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/domain.onmicrosoft.com/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$TokenResponse
$token = $TokenResponse.access_token
$token
$Headers = @{"Authorization" = "Bearer $token"}
$Headers
#Get group List -> working fine
$AadGroupListRequestParams = @{
Method = 'GET'
Uri = 'https://graph.microsoft.com/v1.0/groups'
Headers = @{
'Authorization' = "Bearer $token"
}
}
$AadGroupRequestzzz = Invoke-RestMethod @AadGroupListRequestParams
$AadGroupRequestzzz.value
#Add member to group -> not working
$AadGroupMemberAddRequestParams = @{
Method = 'POST'
Uri = 'https://graph.microsoft.com/v1.0/groups/fb30b950-yyyy-yyyy-yyyy-6015f411ec3c/members/$ref'
Headers = @{
'Authorization' = "Bearer $token"
}
Body = @{
'@odata.id' = "https://graph.microsoft.com/v1.0/directoryObjects/673cf709-yyyy-yyyy-yyyy-c5c476d6yyyy"
}
}
#tried分别传递在param下,仍然没有用 $ params = @{ “@odata.id” =“ https://graph.microsoft.com/v1.0/directoryobjects/673cf709-yyyyyyyyyyyyyyyyyyyy-yyyy-C5C476D6Yyyyy” }
$AadGroupAddMemberRequest = Invoke-RestMethod @AadGroupMemberAddRequestParams -ContentType "application/json" -Verbose
$AadGroupAddMemberRequest.value
错误:
Invoke-restMethod:远程服务器返回错误:(400)不良请求。
I followed azure documentation and created following request to add member to AD group using powershell script. But getting error Bad request.
$clientId = "d4b2ca57-yyyy-yyyy-zzzz-50a4ca126390"
$clientSecret = "nnr7Q~zzzzz"
$tenantID = "4f6eyyyy-yyyy-yyyy-yyyy-0981d022yyyy"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientId
Client_Secret = $clientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/domain.onmicrosoft.com/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$TokenResponse
$token = $TokenResponse.access_token
$token
$Headers = @{"Authorization" = "Bearer $token"}
$Headers
#Get group List -> working fine
$AadGroupListRequestParams = @{
Method = 'GET'
Uri = 'https://graph.microsoft.com/v1.0/groups'
Headers = @{
'Authorization' = "Bearer $token"
}
}
$AadGroupRequestzzz = Invoke-RestMethod @AadGroupListRequestParams
$AadGroupRequestzzz.value
#Add member to group -> not working
$AadGroupMemberAddRequestParams = @{
Method = 'POST'
Uri = 'https://graph.microsoft.com/v1.0/groups/fb30b950-yyyy-yyyy-yyyy-6015f411ec3c/members/$ref'
Headers = @{
'Authorization' = "Bearer $token"
}
Body = @{
'@odata.id' = "https://graph.microsoft.com/v1.0/directoryObjects/673cf709-yyyy-yyyy-yyyy-c5c476d6yyyy"
}
}
#Tried passing below param separately , still no use
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/673cf709-yyyy-yyyy-yyyy-c5c476d6yyyy"
}
$AadGroupAddMemberRequest = Invoke-RestMethod @AadGroupMemberAddRequestParams -ContentType "application/json" -Verbose
$AadGroupAddMemberRequest.value
Error:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请求主体需要采用JSON格式,因为您将参数 contentType 作为
application/json
添加成员 - 请求body
在身体上使用
转换> json
The request body needs to be in JSON format, as you are passing the parameter ContentType as
application/json
Add members - Request Body
Use
ConvertTo-Json
on the body