2

我对 Powershell 很陌生,目前我正在尝试从我从 Solr 下载的 JSON 转换一些数据。

JSON 文件如下所示:

{
 "responseHeader": {"status":0, "QTime":2651},
"InitFailures":{}, 
"status":{
   "YX_SERVER.ONE_LOG.2020-11-07_07_49_33": {
         "name":"SERVER.ONE_LOG.2020-11-07_07_49_33",
            "index":{
               "sizeInBytes":69,
                "size":"69 bytes"}},
 "XY_SERVER.TWO_LOG.2020-11-08_09_52_11": {
         "name":"XY_SERVER.TWO_LOG.2020-11-08_09_52_11",
            "index":{
               "sizeInBytes":6487,
                    "size":"6487 bytes"}},
 "ZX_SERVER.THREE_LOG.2020-10-07_07_42_23": {
         "name":"SERVER.THREE_LOG.2020-10-07_07_42_23",
            "index":{
               "sizeInBytes":6977,
               "size":"6977 bytes"
                              }}}}

第二个状态行下方是具有众多参数的服务器列表(所有名称都不同),我试图深入了解它,但需要嵌套数据的帮助。

我尝试了以下方法:

$list = Get-Content C:\Users\User\Download\cores.json - Raw |convertfrom-json |select-object -ExpandProperty status

然后 $list 向我显示服务器列表,所需的数据在右栏中,但我无法直接访问它。如何构建表格并计算总和,如下所示:

Name                                     Size(inbytes) 
YX_SERVER.ONE_LOG.2020-11-07_07_49_33    69
XY_SERVER.TWO_LOG.2020-11-08_09_52_11    6487
ZX_SERVER.THREE_LOG.2020-10-07_07_42_23  6977

Summary                                  Total size
                                         ?
4

1 回答 1

1
$Content = Get-Content .\cores.json
$Data = $Content | ConvertFrom-Json
$Status = $Data.status
$Table = $Status.PSObject.Properties.Value | Foreach-Object {
    [pscustomobject]@{
        name = $_.name
        sizeInBytes = $_.index.sizeInBytes
    }
}
$Table

[pscustomobject]@{
    name = 'Total Size:'
    sizeInBytes = ($Table.sizeInBytes | Measure-Object -Sum).Sum
}

name                                  sizeInBytes
----                                  -----------
SERVER.THREE_LOG.2020-10-07_07_42_23         6977
XY_SERVER.TWO_LOG.2020-11-08_09_52_11        6487
SERVER.ONE_LOG.2020-11-07_07_49_33             69
Total Size:                                 13533
于 2020-11-26T12:37:10.357 回答