我有以下两个对象,它们来自2个Json文件:
$Env = ConvertFrom-Json "$(get-content "C:\chef\environments.json")"
$Roles = ConvertFrom-Json "$(get-content "C:\chef\roles.json")"
转换后的输出:
PS C:\chef> $Env
run_list
--------
{recipe[djin_chef-max_any::default]}
PS C:\chef> $Roles
7-zip : @{home=%SYSTEMDRIVE%\7-zip}
cookbook_versions :
default : @{env=development}
modmon : @{env=dev}
paypal : @{artifact=%5BINTEGRATION%5D}
seven_zip : @{url=https://djcm-zip-local/djcm/chef}
task_sched : @{credentials=XN$q}
windows : @{password=K1N5}
我需要在powershell中合并这两个Json对象,我尝试了以下内容:
PS C:\chef> $Roles+$Env
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:1 char:1
+ $Roles+$Env
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
如果我做错了还是为什么会出现这个错误,还有另一种优雅的方法吗?
答案 0 :(得分:2)
$Env
只有一个属性,因此您可以向$Roles
添加新成员:
$Roles | Add-Member -NotepropertyName run_list -NotePropertyValue $Env.run_list
此语法在PowerShell v3中有效,但您在标记中列出了v2和v2 ..所以对于v2:
$Roles | Add-Member -MemberType NoteProperty -Name run_list -Value $Env.run_list