0

我有以下对象,我想更改对象中的嵌套值而不更改所有其他嵌套对象。我想更改的对象在一个变量中。

$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

我想做以下事情:

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

我可以使用以下代码读取子对象的内容:

iex "`$object.$change"

但我找不到设置/更改子对象的方法。

代码示例:

$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"
4

1 回答 1

0

这确实有效,我知道调用表达式是纯粹的邪恶,但现在它是我能找到的唯一解决方案。

使用这个要非常小心!!!

Invoke-expression "`$Object.$query = `$newobject”
于 2020-10-20T10:09:03.073 回答