Add a key / value pair to a hash table (nested in an array, nested in a hash table)

Forgive me, but I do not know the correct terminology for this.

Assuming the following hash table:

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

How to do this:

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

I can do:

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

And the execution $ConfgurationData.AllNodeslooks right:

$ConfigurationData.AllNodes

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

But converting it to JSON tells a different story:

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

NewItemand AnotherNewItemare in their own hash table, not in the first one, and this causes the DSC to throw shaky:

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


I can do the following which gives me the correct result:

$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
                     }
                 ]
}

But this seems redundant compared to a type string $ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}

I also tried and failed with:

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

Is there a similar way to target the right “element”?

+4
source share
3 answers

.

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

, -:

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

- @(), $ConfigurationData -, .

gms0ulman , . , :

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

, , :

$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(), +=@{} .

0

Source: https://habr.com/ru/post/1688946/


All Articles