Add key/value pair to hashtable(嵌套在数组中,嵌套在哈希表中)

Add key/value pair to hashtable (nested in an array, nested in a 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"}

有没有类似的方法来定位正确的"element"?

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

$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() 方法而 +=@{} 没有用。

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

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

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

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

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

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

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