繁体   English   中英

将哈希表转换为 json 后,Powershell 缺少数组

[英]Powershell missing array after conversion hashtable to json

使用 Graph API 和 Intune。 我在我的脚本中创建哈希表并将其转换为 POST 到 GraphAPI 的 JSON。 但在转换过程中的一种情况下,我丢失了数组。 因为这个 GraphAPI 不想接受 JSON 并返回错误。

转换正确的情况:

$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在评论中提供了关键提示,但让我把它拼出来:

为确保您的foreach语句的输出是一个数组,请将其括在数组子表达式运算符@()中:

$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] @{ ... } )。

@(...)确保返回一个数组(类型为[object[]] ),无论foreach语句输出多少对象(如果有的话)。

如果没有@(...) ,收集的输出的数据类型取决于语句或命令产生的输出对象的数量

  • 如果没有输出对象,则返回特殊的“AutomationNull”值,表示没有输出; 这个特殊值在枚举上下文中表现得像一个空集合,特别是在管道中,在表达式上下文中就像$null - 有关更多信息,请参见此答案 在当前的上下文中,它将被转换为null JSON 值。

  • 如果有一个输出对象,它会按原样收集,就像它本身一样 - 这就是您所看到的。

  • 只有两个或更多输出对象才能得到一个数组(类型为[object[]] )。

注意:上述行为来自 PowerShell 管道 / 其成功输出流的流式传输行为:对象被逐个发出,并且仅在需要收集输出对象时才需要处理多个对象:请参阅此答案了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM