通过 Powershell 的图形 API

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

我正在尝试通过 Powershell 通过 REST 调用让 Graph API 工作,但我遇到了一些问题。

基本上,我正在尝试通过对 Graph API 使用 Invoke-RestMethod 来创建一个新的 Entra 角色。

我已经制作了我的令牌,它可以工作并且我已经在其他脚本中实现了

    $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/$tenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$TokenAccess = $Tokenresponse.access_token

但是,当我运行以下代码时

$CreateRoleUrl = "https://graph.microsoft.com/v1.0/roleManagement/directory/roleDefinitions"
$CreateRole = Invoke-RestMethod -Uri $CreateRoleUrl -Headers @{Authorization = "Bearer $($TokenAccess)" } -Method Post -Body $($CreateRoleDefinition | convertto-json) -ContentType "application/json"

我收到错误

Invoke-RestMethod: { "error": {"code": "Request_BadRequest",
"message": "An unexpected \u0027PrimitiveValue\u0027 node was found when reading from the JSON reader. A \u0027StartArray\u0027 node was expected.",
"innerError": {
  "date": "2024-04-21T10:21:28",
  "request-id": "a274512b-c4b2-423d-ac2d-e81f4816b067",
  "client-request-id": "a274512b-c4b2-423d-ac2d-e81f4816b067"
}

我相信我的查询中的 JSON 格式是正确的,它是从 PSCustomObject 转换而来,并与 此站点 进行语法比较。

powershell microsoft-entra
1个回答
0
投票

我想通了。转换为 JSON 时,我必须使用“-Depth”参数指定深度,否则使用默认深度 2 并且生成的 JSON 不完整。

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