I have some difficulties to understand how works JSON in PowerShell... I want to create and implement a JSON but I think I do something wrong.
This code not works :
$json = @"
{
"Volume" : [
]
}
"@ | ConvertFrom-Json | Select-Object -Expand Volume
foreach ($i in 0..2) {
$blockvalue = @"
{ "UUID" : "uuid-$($i)",
"Details" : [ { "Name" : "Name-$($i)" }, { "SVM" : "SVM-$($i) " }, { "LastSnapshot" : "snap-$($i)" }, { "Date" : "date-$($i) " } ] }
"@
$json += (ConvertFrom-Json -InputObject $blockvalue)
}
$json | Select UUID,@{Name="Volume";E={$_.Details | Select -Expand Name}},@{Name="SVM";E={$_.Details | Select -Expand SVM}},@{Name="Date";E={$_.Details | Select -Expand Date}},@{Name="LastSnapshot";E={$_.Details | Select -Expand LastSnapshot}} | FT
Output :
Method invocation failed because [System.Management.Automation.PSObject] does
not contain a method named 'op_Addition'.
At line:14 char:5
+ $json += (ConvertFrom-Json -InputObject $blockvalue)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], Runtime
Exception
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Management.Automation.PSObject] does
not contain a method named 'op_Addition'.
At line:14 char:5
+ $json += (ConvertFrom-Json -InputObject $blockvalue)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], Runtime
Exception
+ FullyQualifiedErrorId : MethodNotFound
UUID Volume SVM Date LastSnapshot
---- ------ --- ---- ------------
uuid-0 Name-0 SVM-0 date-0 snap-0
So I find a "solution", I use this next code, it's work but it's not clean (and the worst it's idk why it's works) :
$json2 = @"
{
"Volume" : [
{},
{}
]
}
"@ | ConvertFrom-Json | Select-Object -Expand Volume
foreach ($i in 0..2) {
$blockvalue = @"
{ "UUID" : "uuid-$($i)",
"Details" : [ { "Name" : "Name-$($i)" }, { "SVM" : "SVM-$($i) " }, { "LastSnapshot" : "snap-$($i)" }, { "Date" : "date-$($i) " } ] }
"@
$json2 += (ConvertFrom-Json -InputObject $blockvalue)
}
$json2 | Select UUID,@{Name="Volume";E={$_.Details | Select -Expand Name}},@{Name="SVM";E={$_.Details | Select -Expand SVM}},@{Name="Date";E={$_.Details | Select -Expand Date}},@{Name="LastSnapshot";E={$_.Details | Select -Expand LastSnapshot}} | FT
Output :
UUID Volume SVM Date LastSnapshot
---- ------ --- ---- ------------
uuid-0 Name-0 SVM-0 date-0 snap-0
uuid-1 Name-1 SVM-1 date-1 snap-1
uuid-2 Name-2 SVM-2 date-2 snap-2
See what I'm expected :
UUID Volume SVM Date LastSnapshot
---- ------ --- ---- ------------
uuid-0 Name-0 SVM-0 date-0 snap-0
uuid-1 Name-1 SVM-1 date-1 snap-1
uuid-2 Name-2 SVM-2 date-2 snap-2
I'm sure that it easy to solve but I not gifted with PS...
Thanks by advance for your help :)