I'm trying to add an array of hashtables to an existing array of hastables in Powershell, adding a single item works fine but I can't seem to find out how to add multiple hashtables to an existing array.
For example I have the below;
$ToAdd = @()
$ToAdd += (@{
id = "1234";
type = "Scope";
})
I then want to be able to add the $ToAdd array of hashtables to the object below ($helloBody.helloAccess.AccessRequired)
$HelloBody = [ordered] @{
"helloAccess" = @(
@{
hello = "hello"
accessRequired = @(
@{
id = "random guid"
type = "item 123"
}
)
}
)
}
How is it possible to add an array of hashtables into another array?
When I try $helloBody.helloAccess.AccessRequired += $ToAdd, I get the following error:
A hash table can only be added to another hash table.
$helloBody.helloAccess.AccessRequired = $ToAdd(or$helloBody.helloAccess.AccessRequired += $ToAdd) not working?At line:1 char:1 + $UpdateAppBody.requiredResourceAccess.resourceAccess += $test + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : AddHashTableToNonHashTable$ToAddinto the $helloBody.helloAccess.AccessRequired object that gives me the desired output however I need to be able to achieve this programmatically. The$ToAddarray of hashtables will be created fro objects from aForEach-Objectand then added into the main body which will later be POST via HTTP rest method