PowerShell 51函数没有显示预期的输出



我正在测试反序列化和溅射,但没有从函数Function1获得预期的输出

反序列化(未按预期工作)

$pick1 = 'red,blue' -split ","
$favoriteColors = @()
$favoriteColors += $pick1
$payload = @{}
$payload.add('DEV',@{firstName='Rod';favoriteColors=$favoriteColors})
# Serialize 
$serialPayload = $payload | ConvertTo-Json -compress
# Deserialize test
$payload2 = @{}
(ConvertFrom-Json $serialPayload).psobject.properties | Foreach { $payload2[$_.Name] = $_.Value }
function Main(){
$test = $payload2['DEV']
Function1 @test
}
function Function1([object] $favoriteColors){
"Value1: $($favoriteColors)"
}
Main

I was expected:

"red,blue" string

但是我得到了整个payload2['DEV']对象,而不是我期望的飞溅。

Value1: @{favoriteColors=System.Object[]; firstName=Rod} 

未序列化(按预期工作)

$pick1 = 'red,blue' -split ","
$favoriteColors = @()
$favoriteColors += $pick1
$payload = @{}
$payload.add('DEV',@{firstName='Rod';favoriteColors=$favoriteColors})
function Main(){
$test = $payload['DEV']
Function1 @test
}
function Function1([object] $favoriteColors){
"Value: $($favoriteColors)"
}
Main

输出:

Value: red blue

我必须重新序列化然后反序列化,反馈?

$pick1 = 'red,blue' -split ","
$favoriteColors = @()
$favoriteColors += $pick1
$pick2 = 'green,yellow' -split ","
$favoriteColors2 = @()
$favoriteColors2 += $pick2
$payload = @{}
$payload.add('DEV',@{firstName='Rod';favoriteColors=$favoriteColors})
$payload.add('UAT',@{firstName='Char';favoriteColors=$favoriteColors2})
# Serialize 
$serialPayload = $payload | ConvertTo-Json -compress
# Deserialize test
$payload2 = @{}
(ConvertFrom-Json $serialPayload).psobject.properties.Value.psobject.Properties | Foreach { $payload2[$_.Name] = $_.Value }
function Main(){
Function1 @payload2
}
function Function1([object] $favoriteColors){
"Value1: $($favoriteColors)"
}
Main

相关内容

  • 没有找到相关文章

最新更新