无法在嵌套属性上添加值

时间:2017-06-05 09:09:53

标签: json powershell

我正在努力修改深深嵌套在PSObject中的属性数据。如果我尝试修改值,Powershell会抛出一个例外,即它无法找到值,但如果我只是调用值本身就可以找到它而没有问题。

json对象的原始数据如下所示:

[
    {
        "@CREATED": "2017-05-04 08:36:23",
        "@LAST_MODIFIED": "2017-05-08 08:18:58",
        "STANDARD_FIELDS": {
            "FIRST_NAME": {
                "@FIELD_ID": 2,
                "@LAST_MODIFIED": "2017-05-04 08:36:24",
                "@VALID_FROM": "2017-05-04",
                "value": "Example"
            },
            "LAST_NAME": {
                "@FIELD_ID": 3,
                "@LAST_MODIFIED": "2017-05-04 08:36:24",
                "@VALID_FROM": "2017-05-04",
                "value": "User"
            },
            "ORGANIZATION": {
                "@FIELD_ID": 8,
                "@MODE": 0,
                "@LAST_MODIFIED": "2017-05-04 08:36:24",
                "@VALID_FROM": "2017-05-04",
                "value": "Example Org"
            },
            "NAME": {
                "@FIELD_ID": 1,
                "@MODE": 1,
                "@LAST_MODIFIED": "2017-05-04 08:36:24",
                "@VALID_FROM": "2017-05-04",
                "@ID": 240
            },
            "SUPERVISOR": {
                "@FIELD_ID": 9,
                "@MODE": 4,
                "@LAST_MODIFIED": "2017-05-04 15:39:30",
                "@VALID_FROM": "2017-05-04",
                "NAME": "Example User2",
                "SYSTEM_ID": "198"
            },
            "JOB_LOCATION": {
                "@FIELD_ID": 14,
                "@MODE": 0,
                "@LAST_MODIFIED": "2017-05-04 14:26:55",
                "@VALID_FROM": "2017-05-04",
                "value": "Stockholm"
            }
        },
        "CUSTOM_FIELDS": {
            "FIELD": [
                {
                    "@FIELD_ID": 1033,
                    "@MODE": 0,
                    "@LAST_MODIFIED": "2017-05-24 10:15:24",
                    "@VALID_FROM": "2017-05-24",
                    "NAME": "Department",
                    "VALUE": {
                        "value": "Example Department"
                    }
                },
                {
                    "@FIELD_ID": 1034,
                    "@MODE": 0,
                    "@LAST_MODIFIED": "2017-05-04 08:36:24",
                    "@VALID_FROM": "2017-05-04",
                    "NAME": "VPN",
                    "VALUE": {
                        "value": "Yes"
                    }
                },
                {
                    "@FIELD_ID": 1036,
                    "@LAST_MODIFIED": "2017-05-04 08:36:24",
                    "@VALID_FROM": "2017-05-04",
                    "NAME": "Titel",
                    "VALUE": {
                        "value": "Test-User"
                    }
                },
                {
                    "@FIELD_ID": 1022,
                    "@MODE": 0,
                    "@LAST_MODIFIED": "2017-05-04 16:43:18",
                    "@VALID_FROM": "2017-05-04",
                    "NAME": "Authorizations",
                    "VALUES": {
                        "VALUE": [
                            {
                                "value": "Auth 1"
                            },
                            {
                                "value": "Auth 2"
                            },
                            {
                                "value": "Auth 3"
                            },
                            {
                                "value": "Auth 4"
                            }
                        ]
                    }
                }
            ]
        }
    }
]

我目前的代码:

$users = get-content "C:\Working-Branch\Temp\NotFoundUsers - Copy.txt"| ConvertFrom-Json



foreach($user in $users){
  $user.CUSTOM_FIELDS.FIELD.values.Value.value
  $NewValue = $user.CUSTOM_FIELDS.FIELD.values.Value.value
  $NewValue += "Test"
  $NewValue
  $($user.CUSTOM_FIELDS.FIELD.values.Value).value = $newValue
}

这导致以下输出:

Auth 1
Auth 2
Auth 3
Auth 4
Auth 1
Auth 2
Auth 3
Auth 4
Test
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At line:10 char:5
+   $($user.CUSTOM_FIELDS.FIELD.values.Value).value = $newValue
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

所以我知道属性存在是因为我调用它两次,一次直接在Object上,一次在我添加值“test”之后。但是当我尝试更新Orignal对象时,它说它无法在对象上找到值“Value”。

我尝试按照This post中的建议进行操作,但对我来说仍然失败。我都使用“+ =”运算符和“=”运算符。两者都有相同的错误。

Result of Get-member

1 个答案:

答案 0 :(得分:2)

我认为你需要找到成员Values

的字段
$newvalue =@{
    "value" = "Test"
    }

foreach($user in $users){

  $array = $user.CUSTOM_FIELDS.FIELD.values.value
  $array += $newvalue

  foreach ($field in $user.CUSTOM_FIELDS.FIELD)
  {
    if ($field.values -ne $null)
    {
        $field.values.value = $array
    }
  }

  $user.CUSTOM_FIELDS.FIELD.values.Value
}

然后$user.CUSTOM_FIELDS.FIELD.values.Value的输出是

value 
----- 
Auth 1
Auth 2
Auth 3
Auth 4
Test