Building a dynamic array of hastables

Welcome Forums General PowerShell Q&A Building a dynamic array of hastables

Viewing 2 reply threads
  • Author
    Posts
    • #212760
      Participant
      Topics: 1
      Replies: 1
      Points: -4
      Rank: Member

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

      set-ceblueprint
      -InstanceId $machine.id
      -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"}

      -confirm:$false
      
      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:
      
      
      $machines = get-cemachine 83b8f5ad-308f-4b70-805f-55e9a8f19f2c
      foreach ($machine in $machines)
      {
      $numberofdisks = $machine.sourceProperties.disks | select-object  @{Name = "type"; Expression = { "SSD" } },name
      $diskhash = $null
      $diskhash = @{}
      foreach ($disk in $numberofdisks) {$diskHash.add($disk.name,$disk.type)}
      $diskHash
      set-ceblueprint 

      -InstanceId $machine.id

      -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.

      • This topic was modified 2 months, 1 week ago by Duane Haas.
    • #212787
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      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.

      IE

      $diskhashes = @()
      foreach ($disk in $numberofdisks) {
          $diskhash = @{}
          $diskHash['name'] = $disk.name
          $diskHash['type'] = $disk.type
      
          $diskhashes+=@($diskHash)
      }
    • #212799
      Participant
      Topics: 1
      Replies: 1
      Points: -4
      Rank: Member

      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) {

      $diskArray.Add(@{
      name = $disk.name
      type = “SSD”
      })
      }

      Thanks for the time @curtis

Viewing 2 reply threads
  • You must be logged in to reply to this topic.