Forgive me, but I don't know the correct terminology for this.
Assuming the following hashtable:
$ConfigurationData = @{
AllNodes = @(
@{
NodeName="*"
PSDscAllowPlainTextPassword=$True
PsDscAllowDomainUser=$True
}
)
}
How do I make it look like this:
$ConfigurationData = @{
AllNodes = @(
@{
NodeName="*"
PSDscAllowPlainTextPassword=$True
PsDscAllowDomainUser=$True
NewItem = "SomeNewValue"
AnotherNewItem = "Hello"
}
)
}
I can do:
$ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}
$ConfigurationData.AllNodes += @{AnotherNewItem = "Hello"}
And doing $ConfgurationData.AllNodes looks 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"
}
]
}
NewItem and AnotherNewItem are in their own hashtable and not in the first one and this causes DSC to throw a wobbly:
ValidateUpdate-ConfigurationData : all elements of AllNodes need to be hashtable and has a property NodeName.
I can do the following which gives me the right 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 overkill, compared to a line like $ConfigurationData.AllNodes += @{NewItem = "SomeNewValue"}
I've also tried and failed with:
$ConfigurationData.AllNodes.GetEnumerator() += @{"NewItem" = "SomeNewValue"}
Is there a similar way to target the right "element"?