使用图形API和Intune.我在脚本中创建哈希表,并将其转换为JSON,并将其发布到GraphAPI.

转换正确时的情况:

$TargetGroupIDs = @('111', '222')
$AppAssignment = @{
  mobileAppAssignments = @{
  }
}

$AppAssignment.mobileAppAssignments = foreach ($GroupID in $TargetGroupIDs) {
  $Hash = [ordered]@{
    "@odata.type" = "#microsoft.graph.mobileAppAssignment"
    intent        = "Required"
    settings      = $null
    target        = @{
      "@odata.type" = "#microsoft.graph.groupAssignmentTarget"
      groupId       = "$GroupID"
    }
  }
  write-output (New-Object -Typename PSObject -Property $hash)
}

$AppAssignment | ConvertTo-Json -Depth 50

输出:

{
  "mobileAppAssignments": [
    {
      "@odata.type": "#microsoft.graph.mobileAppAssignment",
      "intent": "Required",
      "settings": null,
      "target": {
        "groupId": "111",
        "@odata.type": "#microsoft.graph.groupAssignmentTarget"
      }
    },
    {
      "@odata.type": "#microsoft.graph.mobileAppAssignment",
      "intent": "Required",
      "settings": null,
      "target": {
        "groupId": "222",
        "@odata.type": "#microsoft.graph.groupAssignmentTarget"
      }
    }
  ]
}

但当我在$TargetGroupIDs中有一个元素时,转换是不正确的:

$TargetGroupIDs = @('111')
$AppAssignment = @{
  mobileAppAssignments = @{
  }
}

$AppAssignment.mobileAppAssignments = foreach ($GroupID in $TargetGroupIDs) {
  $Hash = [ordered]@{
    "@odata.type" = "#microsoft.graph.mobileAppAssignment"
    intent        = "Required"
    settings      = $null
    target        = @{
      "@odata.type" = "#microsoft.graph.groupAssignmentTarget"
      groupId       = "$GroupID"
    }
  }
  write-output (New-Object -Typename PSObject -Property $hash)
}

$AppAssignment | ConvertTo-Json -Depth 50

输出:

{
  "mobileAppAssignments": {
    "@odata.type": "#microsoft.graph.mobileAppAssignment",
    "intent": "Required",
    "settings": null,
    "target": {
      "groupId": "111",
      "@odata.type": "#microsoft.graph.groupAssignmentTarget"
    }
  }
}

请注意mobileAppAssignments后括号中的差异.在第一种情况下[],但在第二种情况下{}.

有人能告诉我在第二种情况下遗漏了什么吗?

推荐答案

TheoSantiago Squarzon在 comments 中提供了关键提示,但让我详细说明一下:

To ensure that the output from your 100 statement is an array, enclose it in 102, the 101:

$AppAssignment.mobileAppAssignments = @(
  foreach ($GroupID in $TargetGroupIDs) {
    [pscustomobject] @{
      "@odata.type" = "#microsoft.graph.mobileAppAssignment"
      intent        = "Required"
      settings      = $null
      target        = @{
        "@odata.type" = "#microsoft.graph.groupAssignmentTarget"
        groupId       = "$GroupID"
      }
    }
  }
)

另请注意简化语法自定义对象文字语法([pscustomobject] @{ ... })

@(...)确保返回array([object[]]类型),而不管foreach语句输出多少个对象(如果有).

Without 100, the data type of the collected output depends on the number of output objects由语句或命令生成:

  • 如果有no个输出对象,则返回特殊的"AutomationNull"值,表示没有输出;这个特殊值在枚举上下文(尤其是管道)中的行为类似于empty collection,在表达式上下文中的行为类似于$null——有关更多信息,请参见this answer;在当前上下文中,它将转换为null JSON值.

  • 如果有one个输出对象,则收集as-is个,就像它本身一样-这就是您所看到的.

  • 只有two or more个输出对象才能得到array(类型[object[]]).

注:behavior above follows from the streaming behavior of the PowerShell pipeline / its success output stream:对象发出one by one个,需要处理multiple个对象,只有在需要collect个输出对象时才起作用:有关更多信息,请参阅this answer.

Json相关问答推荐

您可以使用Jolt对JSON执行OR条件吗

如何在使用GO时检测JSON中不需要的字段?

如何获取 JSON 对象字段值和同一 JSON 对象的下一个数组中的字段值?

如何使NiFi将数据库单列中的多个值转化为Solr索引中的数组?

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

JOLT 获取带有动态键的数组

使用 jq 和 awk 拆分大型 JSON 文件

使用 Javascript 判断 JSON 对象是否包含值

为什么在我们有 json_encode 时使用 CJSON 编码

Android 上的 JSON - 序列化

在 JSON 字符串中react i18n 换行符

Select 什么数据类型json或者jsonb或者text

如何访问 JSON 对象数组的第一个元素?

POST:在 url 本身中发送 post 请求

python追加到json对象中的数组

从调试器获取 IntelliJ Idea 中的 JSON 对象

使用 axios 在 POST multipart/form-data 请求中发送文件和 json

从 JSON 中 Select 不同的值

使用 JSONArray 和 JSONObject 进行 Foreach

将 Pandas 数据框转换为嵌套 JSON