更改Powershell对象中的嵌套对象值

时间:2020-10-16 09:25:15

标签: powershell object subobject

我有以下对象,并且我想在不更改所有其他嵌套对象的情况下更改对象中的嵌套值。我要更改的对象在变量中。

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

1 个答案:

答案 0 :(得分:0)

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

请谨慎使用!!!

Invoke-expression "`$Object.$query = `$newobject”