enumerate hashtable output

Welcome Forums General PowerShell Q&A enumerate hashtable output

Viewing 2 reply threads
  • Author
    Posts
    • #207732
      Participant
      Topics: 9
      Replies: 9
      Points: 41
      Rank: Member

      Hello,

      I’m having trouble when trying to output data from a hashtable. Below is my approach:

      $CimSession=New-CimSession -ComputerName 'localhost'
      
      
      foreach ($Session in $CimSession) {
          $DiskModel = Get-CimInstance -ClassName Win32_DiskDrive
          $DeviceID = Get-CimInstance -ClassName Win32_LogicalDisk
      }
      
      ForEach-Object `
          -Begin { `
      $HashOut = @{ } ; $Result = @() }  `
          -Process { `
      $HashOut.DiskModel = $DiskModel.Model ; $HashOut.DeviceID = $DeviceID.DeviceID }`
          -End { `
      $Result += [pscustomobject]$HashOut } 
      

      My object ($Result) output is shown in brackets as a “nested” hash table.

      DiskModel                                                                                                                                                    DeviceID
      ---------                                                                                                                                                          --------
      {SAMSUNG MZVLB512HAJQ-000H7,                                                                                               {C:, D:, E:, F:…}
      WDC WD1002FAEX-00Z3A0, LITEON CV1-8B256-HP, WDC WD3202ABYS-01B7A0…} 
      

      My attempt to enumerate:

          foreach ($h in $Result.GetEnumerator()) {
              $($h.DiskModel), $($h.DeviceID)
          }
      

      but output still is not what I was aiming:

      SAMSUNG MZVLB512HAJQ-000H7
      WDC WD1002FAEX-00Z3A0
      LITEON CV1-8B256-HP
      WDC WD3202ABYS-01B7A0
      MTFDDAK256MBF-1AN1ZABHA
      WDC WD2003FZEX-00Z4SA0
      C:
      D:
      E:
      F:
      G:
      J:
      

      I would like under diskmodel property all the disk models to be listed and under DeviceID each DeviceID per disk.

      • This topic was modified 3 months ago by mich.a.
      • This topic was modified 2 months, 4 weeks ago by mich.a.
      • This topic was modified 2 months, 4 weeks ago by mich.a.
      • This topic was modified 2 months, 4 weeks ago by James Petty.
    • #207747
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      I think you actually asked the wrong question. 😉 It takes a little more logic to match a logical drive to the physical disk it’s contained in. Try the following snippet:

      $diskDriveList = Get-CimInstance -ClassName Win32_DiskDrive
      $logicalDiskToPartition = Get-CimInstance -ClassName Win32_LogicalDiskToPartition
      $diskDriveToDiskPartitionList = Get-CimInstance -ClassName Win32_DiskDriveToDiskPartition
      
      foreach ($diskDrive in $diskDriveList) {
          
          $partitionList = $diskDriveToDiskPartitionList | 
              Where-Object { 
                  $_.Antecedent.DeviceID -eq $diskDrive.DeviceID 
              }
      
          $logicalDriveLetterList = $logicalDiskToPartition | 
              Where-Object { 
                  $_.Antecedent.deviceID -in $partitionList.dependent.DeviceId 
              }
      
          [PSCustomObject]@{
              Model       = $diskDrive.Model
              DriveLetter = $logicalDriveLetterList.Dependent.DeviceId -join ','
          }
      }

      Off topic: May I ask what kind of code editor you’re using to paste the code here? Your code looks weird with all these strange spaces.

    • #208536
      Participant
      Topics: 9
      Replies: 9
      Points: 41
      Rank: Member

      Hi Olaf!
      thanks for your answer on this! Somehow forum bot blocked my post for a couple of hours and delayed to reply back. I have to admit this is a total different approach. I was thinking that, since I’ve located which info I needed (i.e. deviceID) it was a matter of output (hence the title in my post) rather than logic. 

      Regarding the code editor, actually is from vscode then to a notepad and then here. Not sure why there are that many spaces.

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