You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将拆分的JSON Schema与Values合并为单个PowerShell对象

解决Intune Graph API返回数据转PowerShell可操作对象数组的问题

核心结论

是的,你需要手动遍历schema列名数组和values数据数组,将二者映射后生成带明确属性名的PSCustomObject数组,这样就能直接按设备名、安装状态等属性查询数据。

具体实现代码

假设你通过Invoke-RestMethod获取的报告数据存储在$reportData变量中,以下是完整的转换逻辑:

# 调用Graph API获取应用安装状态报告(替换为你的实际请求参数)
$authHeaders = @{Authorization = "Bearer $yourAccessToken"}
$reportData = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/deviceManagement/reports/getAppInstallStatusReport(appId='YOUR-TARGET-APP-ID')" -Headers $authHeaders -Method Get

# 提取列名集合和设备数据集合
$columnNames = $reportData.schema.name
$deviceRawData = $reportData.values

# 遍历原始数据,构建结构化对象
$structuredDeviceList = foreach ($deviceRow in $deviceRawData) {
    $deviceProperties = [ordered]@{}
    for ($index = 0; $index -lt $columnNames.Count; $index++) {
        $deviceProperties[$columnNames[$index]] = $deviceRow[$index]
    }
    [PSCustomObject]$deviceProperties
}

# 示例:筛选已安装该应用的设备
$installedDevices = $structuredDeviceList | Where-Object { $_.'Install Status' -eq 'Installed' }
# 示例:仅查看设备名和安装日期
$installedDevices | Select-Object 'Device Name', 'Installed Date'

代码说明

  • 使用[ordered]@{}可以保证对象属性顺序与schema列名顺序一致(可选,不影响查询逻辑)
  • 通过索引将schema中的列名和values中的对应位置数据一一绑定,生成标准PowerShell对象
  • 转换完成后,可直接使用Where-Object、Select-Object等原生PowerShell命令处理数据

额外优化建议

如果返回的原始数据中存在需要类型转换的字段(比如日期字符串、数字字符串),可以在映射时添加转换逻辑:

# 示例:将"Installed Date"字段转换为DateTime类型
$deviceProperties[$columnNames[$index]] = if ($columnNames[$index] -eq 'Installed Date' -and $deviceRow[$index]) {
    [DateTime]$deviceRow[$index]
} else {
    $deviceRow[$index]
}

内容的提问来源于stack exchange,提问作者Eds

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:59:54