PowerShell -JSON/pScustomObject-为什么我的数组会被扁平化为一个对象?

发布于 2025-01-18 19:00:25 字数 3924 浏览 0 评论 0原文

我的输出中的 owners 键(请参阅 OutputFile)我期望作为行分隔的数组,但它输出为单行空格分隔的对象/字符串

脚本:

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.'新应用程序'正如预期的那样

$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"
                                          }
                         }
}

The owners key in my output (see OutputFile) I'm expecting as a line separated array, but it's outputting as a single-line space separated object/string

Script:

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

OutputFile:

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

Desired Output:

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

$owners Variable Examined:

$owners
user1
user2
user3

$owners.gettype()

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

When I look at $outputFile.'New Applications' it's as expected

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

When I look at $outputFile it's flattened

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2025-01-25 19:00:25

关于您问题的最讨人喜欢的解释是-depth正在使用默认值。我已经存储了$ outputfile.'new应用程序'$ json variable的示例中。

[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"
      ]
    }
  }
}

The most likable explanation for your issue is that -Depth is using the default Value. I have stored $outputFile.'New Applications' in the $json variable for below example.

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

Results in:

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

Worth pointing out that the Warning Message is only displayed on newer versions of PowerShell (PS 7.1+ to be precise, thanks mklement0 for pointing it out). Windows PowerShell defaults to truncate the JSON without any warning.


However if we add 1 depth level (Default -Depth value is 2):

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

Results in:

{
  "New Applications": {
    "TestSPN1": {
      "app_id": "asdfdsfad",
      "owners": [
        "user1",
        "user2",
        "user3"
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文