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

如何在PowerShell中读取JSON数组名称并按分组标题列出内容

PowerShell 读取IIS Features JSON并分组输出示例

示例JSON文件结构

假设你的JSON文件(比如IISFeatures.json)结构如下:

{
  "CommonHttpFeatures": [
    "DefaultDocument",
    "DirectoryBrowsing",
    "HttpErrors",
    "StaticContent"
  ],
  "HealthandDiagnostics": [
    "HttpLogging",
    "RequestMonitor",
    "Tracing"
  ],
  "ApplicationDevelopment": [
    "AspNetCoreModule",
    "NetExtensibility",
    "AspNet"
  ]
}

完整PowerShell脚本

# 指定JSON文件路径
$jsonFilePath = "C:\IISFeatures.json"

# 读取并解析JSON内容
$iisFeatureGroups = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json

# 遍历每个分组并输出
foreach ($group in $iisFeatureGroups.PSObject.Properties) {
    # 输出分组标题(加粗显示)
    Write-Host "`n**$($group.Name)**`n" -ForegroundColor Cyan
    # 输出该分组下的所有组件
    $group.Value | ForEach-Object {
        Write-Host "- $_" -ForegroundColor Gray
    }
}

脚本说明

  1. 读取JSON:Get-Content -Raw读取整个文件内容,ConvertFrom-Json将JSON字符串转为PowerShell可操作的对象
  2. 遍历分组:通过PSObject.Properties获取JSON对象的所有属性(即分组标题)
  3. 格式化输出:用Write-Host设置不同颜色区分标题和组件,也可以改用Write-Output将内容输出到文件(比如| Out-File -Path "IISFeaturesList.txt")

自定义调整

  • 如果JSON顶层是数组结构,需要修改遍历逻辑,比如先遍历数组元素再处理分组
  • 若要输出到文件,替换Write-Host为Write-Output,并在脚本末尾添加输出重定向
  • 可调整颜色、缩进等格式以符合个人需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:12:34