PowerShell -JSON/pScustomObject-为什么我的数组会被扁平化为一个对象?
我的输出中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您问题的最讨人喜欢的解释是
-depth
正在使用默认值。我已经存储了$ outputfile.'new应用程序'
在$ json
variable的示例中。结果:
值得指出的是,警告消息仅显示在PowerShell的较新版本上(PS 7.1+是准确的,谢谢 mklement0 指出它)。 Windows PowerShell默认会截断JSON,而无需任何警告。
但是,如果我们添加1个深度级别(默认
-depth
值是 2 ):结果:
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.Results in:
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):Results in: