在powershell中显示Json嵌套数组

时间:2018-06-17 06:02:11

标签: json powershell

我已经编写了一个带有嵌套数组的JSON脚本,并试图根据标题显示所有数据。但不幸的是,我无法做到这一点。我在下面放置了我的JSON和PowerShell代码。请帮助我。

JSON代码:

{
  "Deploy": {
    "ENV": "DIT",
    "Servers": [
      {
        "Webservers": [
          {
            "Server": ["Server1","Server2"],
            "site": ["website1","WebSite2"],
            "VDIR": ["WebSite1","WebSite2"]
          },
          {
            "Server": ["Server1","Server2"],
            "site": ["WebSite1","WebSite2"],
            "VDIR": ["WebSite1","WebSite2"]
          }
        ]
      },
      {
        "Winservers": [
          {
            "Server": ["Server1","Server2"],
            "winService": ["service1","service2"]
          },
          {
            "Server": ["Server3","Server4"],
            "winService": ["service3","service4"]
          }
        ]
      }
    ]
  }
}

Powershell代码:

$Deploy=(Get-Content D:\Deploy.json -Raw) -join "`n"|ConvertFrom-Json

foreach($i in $Deploy.Deploy.Servers) {
    $s=($i | Get-Member -MemberType *property).Name
    Write-host "$s" -ForegroundColor Yellow
    foreach($j in $s) {
        $items=($i.$j|Get-Member -MemberType *property).Name
        Write-host "$items" -ForegroundColor Yellow
        foreach($k in $items) {
            $z=($j.$k | Get-Member -MemberType *property).Name
            Write-host "$z" -ForegroundColor Yellow
        }
    }
}

目前的结果是:

Webservers
Server site VDIR
Winservers
Server winService

但预期结果应如下所示:

Webservers:
Server:                      Sites:            VDIR:
list of servers              list of sites      list of VDIR

Server:                      Sites:            VDIR:
list of servers              list of sites      list of VDIR
Winservers:
Server:                      services:            
list of servers              list of sites      

请帮助......我以不同的方式尽力而为,但没有人给出预期的结果。

3 个答案:

答案 0 :(得分:0)

行。我认为你的问题可以通过下一个方式解决。

$ht, $src = @{}, 'D:\sandbox\source.xml'
($srv = (Get-Content $src | ConvertFrom-Json).Deploy.Servers).ForEach{
   $name = $_.PSObject.Properties.Name
   $ht[$name] = $srv.$name
}

Write-Host Webservers -ForegroundColor Yellow
$ht.Webservers | Out-String

Write-Host Winservers -ForegroundColor Yellow
$ht.Winservers | Out-String

答案 1 :(得分:0)

如果你想要实现的只是一个格式化的输出,也许这更符合你的喜好

$deploy = Get-Content 'D:\Deploy.json' -Raw | ConvertFrom-Json

Write-Host "Webservers:`r`n-----------"
$items = @()
foreach ($server in $deploy.Deploy.Servers.Webservers) {
    foreach ($item in $server) {
        $items += New-Object -TypeName PSObject -Property ([ordered]@{
            Server = @($item.Server) -join ', '
            Sites  = @($item.site) -join ', '
            VDIR   = @($item.VDIR) -join ', '
        })
    }
}
$items | Out-String

Write-Host "Winservers:`r`n-----------"
$items = @()
foreach ($server in $deploy.Deploy.Servers.Winservers) {
    foreach ($item in $server) {
        $items += New-Object -TypeName PSObject -Property ([ordered]@{
            Server = @($item.Server) -join ', '
            WinService  = @($item.winService) -join ', '
        })
    }
}
$items | Out-String

输出将如下所示:

Webservers:
-----------

Server           Sites              VDIR              
------           -----              ----              
Server1, Server2 website1, WebSite2 WebSite1, WebSite2
Server1, Server2 WebSite1, WebSite2 WebSite1, WebSite2



Winservers:
-----------

Server           WinService        
------           ----------        
Server1, Server2 service1, service2
Server3, Server4 service3, service4

答案 2 :(得分:0)

一种实用的for-display-only解决方案,它不会输出您描述的确切格式,但是简洁(PSv3 +):

(Get-Content -Raw D:\Deploy.json | ConvertFrom-Json).Deploy.Servers |
  ForEach-Object {
    $serverTypePropName = $_.psobject.properties.Name
    Write-Host "${serverTypePropName}:"
    $_.$serverTypePropName | Format-Table
  }
  • (Get-Content -Raw D:\Deploy.json | ConvertFrom-Json)将JSON输入转换为自定义对象。

  • .Deploy.Server访问数组值属性,其元素是WebserversWinservers属性分别包含服务器详细信息子数组的对象。

  • ForEach-Object脚本块中:

    • $_.psobject.properties.Name提取包含服务器详细信息数组的每个输入对象的一个​​属性(WebserversWinservers)的名称。
    • $_.$serverTypePropName | Format-Table访问该属性并将server-details数组呈现为表格 请注意,显式使用Format-Table是必需的,因为服务器详细信息数组在WebserversWinservers对象之间具有不同的属性结构;如果没有显式格式化/输出命令,则只会呈现第一个数组的属性。

根据您的样本输入,上述结果为:

Webservers:

Server             site                 VDIR
------             ----                 ----
{Server1, Server2} {website1, WebSite2} {WebSite1, WebSite2}
{Server1, Server2} {WebSite1, WebSite2} {WebSite1, WebSite2}


Winservers:

Server             winService
------             ----------
{Server1, Server2} {service1, service2}
{Server3, Server4} {service3, service4}