将键/值对添加到哈希表(嵌套在数组中,嵌套在哈希表中)

时间:2017-11-08 14:30:32

标签: json powershell hashtable

原谅我,但我不知道正确的术语。

假设以下哈希表:

$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName="*"
            PSDscAllowPlainTextPassword=$True
            PsDscAllowDomainUser=$True
        }
    )
}

如何让它看起来像这样:

$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName="*"
            PSDscAllowPlainTextPassword=$True
            PsDscAllowDomainUser=$True
            NewItem = "SomeNewValue"
            AnotherNewItem = "Hello"
        }
    )
}

我能做到:

$ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}
$ConfigurationData.AllNodes += @{AnotherNewItem  = "Hello"}

正在做$ConfgurationData.AllNodes看起来正确:

$ConfigurationData.AllNodes

Name                           Value                                                                                                                                            
----                           -----                                                                                                                                            
NodeName                       *                                                                                                                                                
PSDscAllowPlainTextPassword    True                                                                                                                                             
PsDscAllowDomainUser           True                                                                                                                                             
NewItem                        SomeNewValue                                                                                                                                     
AnotherNewItem                 Hello  

但将其转换为JSON会讲述一个不同的故事:

$ConfigurationData | ConvertTo-Json
{
    "AllNodes":  [
                     {
                         "NodeName":  "*",
                         "PSDscAllowPlainTextPassword":  true,
                         "PsDscAllowDomainUser":  true
                     },
                     {
                         "NewItem":  "SomeNewValue"
                     },
                     {
                         "AnotherNewItem":  "Hello"
                     }
                 ]
}

NewItemAnotherNewItem在他们自己的哈希表中而不在第一个哈希表中,这会导致DSC摇摆不定:

ValidateUpdate-ConfigurationData : all elements of AllNodes need to be hashtable and has a property NodeName.

我可以做以下事情,这给了我正确的结果:

$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName="*"
            PSDscAllowPlainTextPassword=$True
            PsDscAllowDomainUser=$True
        }
    )
}

#$ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}
#$ConfigurationData.AllNodes += @{AnotherNewItem  = "Hello"}

foreach($Node in $ConfigurationData.AllNodes.GetEnumerator() | Where-Object{$_.NodeName -eq "*"}) 
{
            $node.add("NewItem", "SomeNewValue")
            $node.add("AnotherNewItem", "Hello")
}

$ConfigurationData | ConvertTo-Json
{
    "AllNodes":  [
                     {
                         "NodeName":  "*",
                         "PSDscAllowPlainTextPassword":  true,
                         "NewItem":  "SomeNewValue",
                         "AnotherNewItem":  "Hello",
                         "PsDscAllowDomainUser":  true
                     }
                 ]
}

但与$ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}

之类的行相比,这似乎有些过分

我也试过并失败过:

$ConfigurationData.AllNodes.GetEnumerator() += @{"NewItem" = "SomeNewValue"}

是否有类似的方法来定位正确的"元素"?

3 个答案:

答案 0 :(得分:3)

您的问题是因为您在内部哈希表的初始声明@()中放置了$ConfigurationData括号,这使得它成为一个数组。

根据gms0ulman的答案,您需要使用数组索引运算符来访问此数组的索引,然后在那里修改属性。例如,第一个要素:

$ConfigurationData.AllNodes[0].'NewItem' = 'SomeNewValue'
$ConfigurationData.AllNodes[0].'AnotherNewItem' = 'Hello'

答案 1 :(得分:3)

此行正在数组级别添加项目。

$ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}

实际上,您希望添加到数组的第一个元素,这是您的哈希表:

($ConfigurationData.AllNodes)[0] += @{"new item" = "test"}

答案 2 :(得分:0)

实际上,我唯一没有尝试过:

$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName="*"
            PSDscAllowPlainTextPassword=$True
            PsDscAllowDomainUser=$True
        }
    )
}

$ConfigurationData.AllNodes.GetEnumerator().Add("NewItem","SomeNewValue")

$ConfigurationData.AllNodes.GetEnumerator().Add("AnotherNewItem","Hello")

$ConfigurationData | ConvertTo-Json




{
    "AllNodes":  [
                     {
                         "NodeName":  "*",
                         "PSDscAllowPlainTextPassword":  true,
                         "NewItem":  "SomeNewValue",
                         "AnotherNewItem":  "Hello",
                         "PsDscAllowDomainUser":  true
                     }
                 ]
}

我理解GetEnumerator位。它创建了一个索引 - 各种类型,因此PS可以使用这些项目。

但我不知道为什么我必须使用.Add()方法而+=@{}没有工作。

相关问题