0

I have the following object, and I would like to change a nested value in the object without changing all the other nested objects. The object I would like to change is in a variable.

$object.Tophashtable.MidHashtable1.array1[0].linenumber = 1
                                             Value      = Bottom-array1-1
                                   array1[1].linenumber = 2
                                             Value      = Bottom-array1-2
                                   array1[2].linenumber = 3
                                             Value      = Bottom-array1-3
                                   array2[0].linenumber = 1
                                             Value      = Bottom-array2-1
                                   array2[1].linenumber = 2
                                             Value      = Bottom-array2-2
                     MidHashtable2.array3[0].linenumber = 1
                                             Value      = Bottom-array3-1
                                   array3[1].linenumber = 2
                                             Value      = Bottom-array3-2
                                   array3[2].linenumber = 3
                                             Value      = Bottom-array3-3
                                   array4[0].linenumber = 1
                                             Value      = Bottom-array4-1
                                   array4[1].linenumber = 2
                                             Value      = Bottom-array4-2
                     MidHashtable3.array5[0].linenumber = 1
                                             Value      = Bottom-array5-1

I would like to do the following:

  $newobject = @{    
    linenumber = "1"
    value = "newobject-1"}
  $object.$change = $newobject

I'm able to read the content of the subobject with the following code:

iex "`$object.$change"

But I can't find a way to set/change the subobject.

code example:

$object = @{
    tophashtable = @{
        MidHashtable1 = @{
            array1 = @(
                @{
                    linenumber = "1"
                    value = "Bottum-array1-1"
                },
                @{
                    linenumber = "2"
                    value = "Bottum-array1-2"
                },
                @{
                    linenumber = "3"
                    value = "Bottum-array1-3"
                },
                @{
                    linenumber = "4"
                    value = "Bottum-array1-4"
                }
            )
            array2 = @(
                @{
                    linenumber = "1"
                    value = "Bottum-array2-1"
                },
                @{
                    linenumber = "2"
                    value = "Bottum-array2-2"
                }
            )
        }
        MidHashtable2 = @{
            array3 = @(
                @{
                    linenumber = "1"
                    value = "Bottum-array3-1"
                },
                @{
                    linenumber = "2"
                    value = "Bottum-array3-2"
                },
                @{
                    linenumber = "3"
                    value = "Bottum-array3-3"
                },
                @{
                    linenumber = "4"
                    value = "Bottum-array3-4"
                }
            )
            array4 = @(
                @{
                    linenumber = "1"
                    value = "Bottum-array4-1"
                },
                @{
                    linenumber = "2"
                    value = "Bottum-array4-2"
                }
            )
        }
        MidHashtable3 = @{
            array5 = @(
                @{
                    linenumber = "1"
                    value = "Bottum-array5-1"
                },
                @{
                    linenumber = "2"
                    value = "Bottum-array5-2"
                },
                @{
                    linenumber = "3"
                    value = "Bottum-array5-3"
                }
            )
        }
    }
}

$newobject = @(
    @{
        linenumber = "1"
        value = "newobject-1"
    },
    @{    
        linenumber = "2"
        value = "newobject-2"
    }
)

$query = "tophashtable.MidHashtable1.array2"

# This does work to read the content of the subobject true a variable
Invoke-Expression "`$object.$query"

# This does work to set the content of the subobject to the newobject
$object.tophashtable.MidHashtable1.array2 = $newobject

# I would like to set the content of the subobject by the value of the query variable.
# The value of the query variable can be of different length as wel. 
# Sometime's i want to change the array's but sometime's i would like to change the content of the midhashtable's completely so $query would only be "tophashtable.MidHashtable1"
Roel
  • 1
  • 2
  • 2
    Can you please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – marsze Oct 16 '20 at 09:52
  • Just added a "code example" – Roel Oct 16 '20 at 11:49
  • Not an answer to your use case, as I am still trying to come to terms with it. However, just an FYI... [Invoke-Expression considered harmful](https://devblogs.microsoft.com/powershell/invoke-expression-considered-harmful) ... [Why Invoke-Expression is Evil](https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/why-invoke-expression-is-evil). What is really in this... $object? Show the raw data, sanitized fo course. – postanote Oct 17 '20 at 04:42
  • I tried to add a xml dump of the real $object, but it was way to big. It is the config of a pfsense. I'm trying to edit part's of the config and then upload them back up to pfsense. But i don't want to change all the other part's of the config. The dept of the field's in the $object can verry from 1 up to 5 deep. The location of the field i want't to change is in the $query variable. and it's new value is $newobject. sometime's i don't need to change just one field, but i have to change a complete subobject. – Roel Oct 17 '20 at 13:02

1 Answers1

0

This does work, i Know the Invoke-Expression's are pure evil, but for now it's the only solution i can find.

Be verry carefull using this !!!

Invoke-expression "`$Object.$query = `$newobject”
Roel
  • 1
  • 2