Building a dynamic array of hastables

I’m working with a powershell function that looks like the following:

-InstanceId $
-InstanceType "t3.2xlarge"
-SubnetIDs "subnet-0d94c365043c93ac8"
-SecurityGroupIDs "sg-0836f28e55dd2c349"
-Disks @(@{type = "SSD"; name = "c:0"}, @{type="SSD"; name = "e:0"})
-Tags @{Environment = "prddr"; ITOwner = "Platform Engineering"; BusinessOwner = "IT"; ManagedBy = "CloudEndure"; ProjectCode = "Support-DR"}


The parameter -Disk accepts an array of hashtables ([System.Collections.Hashtable[]]$Disks), and the length will depend on how many disks a given machine might have.  I'm able to pull the number of disks and my $variable that contains the number looks like the following:

type name
---- ----
SSD  c:0
SSD  e:0

I'd like to be able to pass in a variable into -Disks so that it dynamically builds depending on what machine I'm creating blueprint for, example of where I'm at:

<pre><code>$machines = get-cemachine 83b8f5ad-308f-4b70-805f-55e9a8f19f2c
foreach ($machine in $machines)
$numberofdisks = $machine.sourceProperties.disks | select-object  @{Name = &quot;type&quot;; Expression = { &quot;SSD&quot; } },name
$diskhash = $null
$diskhash = @{}
foreach ($disk in $numberofdisks) {$diskHash.add($,$disk.type)}

-InstanceId $

-InstanceType “t3.2xlarge” -SubnetIDs "subnet-0d94c365043c93ac8"
-SecurityGroupIDs “sg-0836f28e55dd2c349” -Disks $diskhash
-Tags @{Environment = “prddr”; ITOwner = “Platform Engineering”; BusinessOwner = “IT”; ManagedBy = “CloudEndure”; ProjectCode = “Support-DR”} -confirm:$false }

The cmdlet doesn’t like the input for $diskhash Hoping for some guidance here, a bit of a newb and am struggling to come up with correct method to use.

From what I can tell you are not meeting your own requirement. You said that -disk needs an array of hash, not a single hash. When you go through your foreach loop you are just building a hash called $diskhash. You are not building an array of $diskhash’es.


$diskhashes = @()
foreach ($disk in $numberofdisks) {
    $diskhash = @{}
    $diskHash['name'] = $
    $diskHash['type'] = $disk.type


Apologies for the crap formatting, I was able to figure it out:

$numberofdisks = $machine.sourceProperties.disks
$diskArray = [System.Collections.Generic.List[hashtable]]@()
foreach ($disk in $numberofdisks) {
        name = $
        type = "SSD"

Thanks for the time @curtis