gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:24:10 32 4
gpt4 key购买 nike

使用图谱 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 的输出statement 是一个数组,将它包含在@() 中,array-subexpression operator :

$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 - 参见this answer想要查询更多的信息;在手头的上下文中,它将被转换为 null JSON 值。

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

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

注意:上述行为遵循 PowerShell 管道的行为/它的成功输出流:对象一个接一个地发出 ,并且只有在需要收集输出对象时才需要处理多个对象:参见this answer获取更多信息。

关于arrays - 将哈希表转换为 json 后 Powershell 缺少数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72350326/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com