PowerShell - JSON/PsCustomObject - 为什么我的数组会被压平成单个对象?

PowerShell - JSON/PsCustomObject - Why does my array get flattened into a single object?

我的输出中的 owners 键(参见 OutputFile)我期望作为行分隔数组,但它输出为单行 space分开object/string

脚本:

function Add-ApplicationOwner
{
    param (
        [string] $App,
        [object] $OutputObject
    )

    # add values to our json output
    $owners = (Get-AzureAdApplicationOwner -ObjectId $App).UserPrincipalName
    $OutputObject | Add-Member -MemberType NoteProperty -Name owners -Value $owners
}

$inputFile = Get-Content -Path "AppInput.json" -Raw | ConvertFrom-Json
$outputFile = New-Object -TypeName PsObject

foreach ($object in $inputFile.PSObject.Properties)
{
    $outputAppList = New-Object -TypeName PsObject

    foreach ($app in $inputFile.New.PsObject.Properties)
    {           

        # create app
        $appRegistration = New-AzureADApplication -DisplayName "TestSPN1"

        #add application info into json object
        $outputAppValues = [PsCustomObject]@{
            app_id = $appRegistration.AppId
        }

        #add application owners by object id
        Add-ApplicationOwner -App $appRegistrationObjectId -OutputObject $outputAppValues

        $outputAppList | Add-Member -MemberType NoteProperty -Name "TestSPN1" -Value $outputAppValues
             
    }

    # add all created apps into json output file
    $outputFile | Add-Member -MemberType NoteProperty -Name "New Applications" -Value $outputAppList

}

$outputFile | ConvertTo-Json | Out-File "AzADAppRegistrationInfo.json" -Append

输出文件:

{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "dsfadfdafa-3afadfdafadsfasd-343",
                                              "owners":  "user1 user2 user3"
                                          }
                         }
}

期望输出:

{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "dsfadfdafa-3afadfdafadsfasd-343",
                                              "owners":  [
                                                   "user1",
                                                   "user2",
                                                   "user3"
                                               ]
                                          }
                         }
}

$owners 检查的变量:

$owners
user1
user2
user3

$owners.gettype()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                  
True     True     Object[]                                 System.Array     

当我查看 $outputFile.'New Applications' 时,它符合预期

$outputFile.'New Applications' | convertto-json
{
    "TestSPN1":  {
                     "app_id":  "asdfdsfad",
                     "owners":  [
                                    "user1",
                                    "user2",
                                    "user3"
                                ]
                 }
}

当我查看 $outputFile 时,它​​变平了

$outputFile | convertto-json
{
    "New Applications":  {
                             "TestSPN1":  {
                                              "app_id":  "cc6dgfsdgdsgfsdgfdsa5562614",
                                              "owners":  "user1 user2 user3"
                                          }
                         }
}

对您的问题最恰当的解释是 -Depth 使用的是默认值。对于下面的示例,我已将 $outputFile.'New Applications' 存储在 $json 变量中。

[pscustomobject]@{
    'New Applications' = $json
} | ConvertTo-Json

结果:

WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 2.
{
  "New Applications": {
    "TestSPN1": {
      "app_id": "asdfdsfad",
      "owners": "user1 user2 user3"
    }
  }
}

值得指出的是,警告消息仅显示在较新版本的 PowerShell 上(准确地说是 PS 7.1+,感谢 mklement0 指出)。 Windows PowerShell 默认截断 JSON 而没有任何警告。


然而,如果我们添加 1 个深度级别(默认 -Depth 值为 2):

[pscustomobject]@{
    'New Applications' = $json
} | ConvertTo-Json -Depth 3

结果:

{
  "New Applications": {
    "TestSPN1": {
      "app_id": "asdfdsfad",
      "owners": [
        "user1",
        "user2",
        "user3"
      ]
    }
  }
}