[英]Powershell missing array after conversion hashtable to json
Working with Graph API and Intune.使用 Graph API 和 Intune。 I create hash table in my script and covert it to JSON which POST to GraphAPI.
我在我的脚本中创建哈希表并将其转换为 POST 到 GraphAPI 的 JSON。 But in one case during conversion I lose array.
但在转换过程中的一种情况下,我丢失了数组。 Because of this GraphAPI does not want to accept JSON and returns error.
因为这个 GraphAPI 不想接受 JSON 并返回错误。
Case when conversion is correct:转换正确的情况:
$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
Output:输出:
{
"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"
}
}
]
}
But when I have one element in $TargetGroupIDs conversion is not correct:但是当我在 $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
Output:输出:
{
"mobileAppAssignments": {
"@odata.type": "#microsoft.graph.mobileAppAssignment",
"intent": "Required",
"settings": null,
"target": {
"groupId": "111",
"@odata.type": "#microsoft.graph.groupAssignmentTarget"
}
}
}
Please note difference in brackets after mobileAppAssignments.请注意 mobileAppAssignments 后括号中的差异。 In first case [], but in second case {}.
在第一种情况下是 [],但在第二种情况下是 {}。
Could someone tell what I am missing in second case?有人可以告诉我在第二种情况下我缺少什么吗?
Theo and Santiago Squarzon have provided the crucial hint in the comments, but let me spell it out: Theo和Santiago Squarzon在评论中提供了关键提示,但让我把它拼出来:
To ensure that the output from your foreach
statement is an array , enclose it in @()
, the array-subexpression operator :为确保您的
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"
}
}
}
)
Also note that simplified syntax custom-object literal syntax ( [pscustomobject] @{ ... }
).还要注意简化的语法自定义对象文字语法(
[pscustomobject] @{ ... }
)。
@(...)
ensures that an array (of type [object[]]
) is returned, irrespective of how many objects, if any, the foreach
statement outputs. @(...)
确保返回一个数组(类型为[object[]]
),无论foreach
语句输出多少对象(如果有的话)。
Without @(...)
, the data type of the collected output depends on the number of output objects produced by a statement or command:如果没有
@(...)
,收集的输出的数据类型取决于语句或命令产生的输出对象的数量:
If there is no output object, the special "AutomationNull" value is returned, which signifies the absence of output;如果没有输出对象,则返回特殊的“AutomationNull”值,表示没有输出; this special value behaves like an empty collection in enumeration contexts, notably the pipeline, and like
$null
in expression contexts - see this answer for more information;这个特殊值在枚举上下文中表现得像一个空集合,特别是在管道中,在表达式上下文中就像
$null
- 有关更多信息,请参见此答案; in the context at hand, it would be converted to a null
JSON value.在当前的上下文中,它将被转换为
null
JSON 值。
If there is one output object, it is collected as-is , as itself - this is what you saw.如果有一个输出对象,它会按原样收集,就像它本身一样 - 这就是您所看到的。
Only with two or more output objects do you get an array (of type [object[]]
).只有两个或更多输出对象才能得到一个数组(类型为
[object[]]
)。
Note: The behavior above follows from the streaming behavior of the PowerShell pipeline / its success output stream : object are emitted one by one , and needing to deal with multiple objects only comes into play if there's a need to collect output objects: see this answer for more information.注意:上述行为来自 PowerShell 管道 / 其成功输出流的流式传输行为:对象被逐个发出,并且仅在需要收集输出对象时才需要处理多个对象:请参阅此答案了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.