Invoke-retMethod:远程服务器返回错误:(400)不良请求。 Azure AAD添加组成员

发布于 2025-01-28 03:06:43 字数 1803 浏览 1 评论 0原文

我遵循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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2025-02-04 03:06:43

请求主体需要采用JSON格式,因为您将参数 contentType 作为application/json

添加成员 - 请求body

在请求正文中,提供添加目录的JSON代表,用户,组或组织联系对象。

在身体上使用转换> json

Body = @{
     '@odata.id' = "https://graph.microsoft.com/v1.0/directoryObjects/673cf709-yyyy-yyyy-yyyy-c5c476d6yyyy"
} | ConvertTo-Json

The request body needs to be in JSON format, as you are passing the parameter ContentType as application/json

Add members - Request Body

In the request body, supply a JSON representation of a directoryObject, user, group, or organizational contact object to be added.

Use ConvertTo-Json on the body

Body = @{
     '@odata.id' = "https://graph.microsoft.com/v1.0/directoryObjects/673cf709-yyyy-yyyy-yyyy-c5c476d6yyyy"
} | ConvertTo-Json
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文