unable to add value on nested property

Viewed 91

I am struggling to modify the data of properties that are deeply nested in my PSObject. If I try to modify the values Powershell just throws the exception of saying that it cannot find the value, but if I just call upon the value itself it can be found without an issue.

The raw data of the json object looks like this:

[
    {
        "@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"
                            }
                        ]
                    }
                }
            ]
        }
    }
]

My current code:

$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
}

Which results in the following output:

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

So I know the property exists because I call upon it two times, one directly on the Object and one time after I have added the value "test" to it. But when I try to update the Orignal object it says it can not find the value "Value" on the object.

I tried following the what was suggested in This post, but it still fails for me. I both with the "+=" operator and the "=" operator. Same error with both of them.

Result of Get-member

1 Answers
Related