将pscustomobject转换为Json格式

时间:2019-05-09 01:33:13

标签: powershell pscustomobject

我有示例Json格式,目前正在尝试使用Powershell进行编码,并使用PScustomobject将其转换为Json格式,但是由于DBdetails的数量取决于我要检索的数组,因此我很难。我的逻辑怎么了?

$dbcountall = 1
foreach ($alldblist in $dblist)
{  
if($dbcountall -eq 1)
{
    $name = $alldblist.name
    $dbdetailsname = "DBDetails" + $dbcountall
    $dbpiece = [pscustomobject]@{ 
    SourceType = "$name"
    TargetType = "$name"
    }
    $alldb += [pscustomobject]@{ 
    DataBasesCount = $dblist.count
    $dbdetailsname = $dbpiece
    }
}
else
{
    $name = $alldblist.name
    $dbdetailsname = "DBDetails" + $dbcountall
        $dbpiece = [pscustomobject]@{ 
        SourceType = "$name"
        TargetType = "$name"
        }
    $alldb += [pscustomobject]@{ 
    $dbdetailsname = $dbpiece
    }
}
$dbcountall++
}

##Output is
"Databases":  [
                                   {
                                       "DatabasesCount":  "4",
                                       "DBDetails1":  {
                                                          "Source":  "SampleDB1",
                                                          "Target":  "SampleDB1"
                                                      }
                                   },
                                   {
                                       "DBDetails2":  {
                                                          "Source":  "SampleDB2",
                                                          "Target":  "SampleDB2"
                                                      }
                                   },
                                   {
                                       "DBDetails3":  {
                                                          "Source":  "testdatabase",
                                                          "Target":  "testdatabase"
                                                      }
                                   },
                                   {
                                       "DBDetails4":  {
                                                          "Source":  "Testdatabase_backup_10GB",
                                                          "Target":  "Testdatabase_backup_10GB"
                                                      }
                                   }
                               ],

结果应该看起来像这样,从上方看,它还有一个附加的括号。

"Databases": [
     {
      "DatabasesCount": "3",
      "DBDetails1": {
        "Source": "db1",
        "Target": "db1"
      },
      "DBDetails2": {
        "Source": "db2",
        "Target": "db2"
      },
      "DBDetails3": {
        "Source": "db3",
        "Target": "db3"
      }

    }
  ],

1 个答案:

答案 0 :(得分:2)

创建一个对象,然后为每个数据库添加属性,而不是为每个数据库创建一个对象。

$DatabaseInfo = [pscustomobject]@{
    DatabasesCount = $dblist.count
}
For($i=1;$i -le $dblist.count;$i++){
    $DBObject = [pscustomobject]@{
        Source = $dblist[$i-1].Name
        Target = $dblist[$i-1].Name
    }
    $DatabaseInfo | Add-Member "DBDetails$i" $DBObject
}