PowerShell - Multidimensional Hashtable

Discussion in 'Programming & Software Development' started by birdie, Jun 4, 2018.

  1. birdie

    birdie Member

    Joined:
    Jun 18, 2002
    Messages:
    2,750
    Location:
    Bundaberg, Queensland
    I'm working on a PowerShell script for work, I'm trying to work out the best method to store some data so I can then use it later to output for JS for a ba rgraph.

    The data looks like this:
    Code:
    Name           OS                        Type             Count
    JoeBlogs      Win 2008            DC                5
    JoeBlogs      Win 2008            MS                10
    JoeBlogs      Win 2016            DC                1
    JoeBlogs      Win 2016            MS                3
    TonySmith     Win 2008            DC                9
    TonySmith     Win 2008            MS                3
    TonySmith     Win 2016            DC                5
    TonySmith     Win 2016            MS                13
    SimonSays     Win 2008            DC                14
    SimonSays     Win 2008            MS                25
    BillBlogsie   Win 2008            DC                10
    BillBlogsie   Win 2008            MS                15
    etc
    
    What I'd like to be able to do is store is somehow so that I can refer to is by Name.OS.Type and get the value of Count (so I can loop through the hashtable.

    I'm using graph.js to display the bar graph and I'm using groups, so I want to able to show all Joe Blogs in one group of columns, and Tony Smith in another group, etc and also be able to colour code by the OS and Type.

    What I have been trying is a loop
    Code:
    $HashTable = @{}
    foreach ($row in $data) {
        $HashTable[$row.Name] = @{
            $row.OS = @{
                $row.Type = $row.Count
            }
        }
    }
    
    The issue I have been having is because the OS is not a unique key i only get the last value for each one for example I'll get:
    Code:
    Name           OS                        Type             Count
    JoeBlogs      Win 2008            MS               10
    JoeBlogs      Win 2016            MS               3
    TonySmith     Win 2008            MS               3
    TonySmith     Win 2016            MS               13
    SimonSays     Win 2008            MS               25
    BillBlogsie   Win 2008            MS               15
    etc
    
    Is a Hashtable the right way to go or should I use some other method?
     
  2. Hookimus

    Hookimus Member

    Joined:
    Jan 11, 2016
    Messages:
    40
    Location:
    Sydney
    I like using PSObjects
    Code:
    $PSObject = @()
    foreach ($row in $data) {
        $item = [PSCustomObject]@{
            Name = $row.name
            OS = $row.OS
            Type = $row.Type
            Count = $row.Count
            }
        $PSObject += $item
    }
    
    Then basically just use where objects to filter out what you want
    Code:
    PS C:\Scripts (31) >  $PSObject | ?{($_.Name -eq "JoeBlogs") -and ($_.OS -eq "Win 2008")}
    
    Name     OS       Type Count
    ----     --       ---- -----
    JoeBlogs Win 2008 DC   5
    JoeBlogs Win 2008 MS   10
    
    You could probably do something similiar with hashtables, I've just not had much experience with them as I would normally gravitate to Arrays \ Custom PS Objects
     
    PabloEscobar likes this.
  3. Hookimus

    Hookimus Member

    Joined:
    Jan 11, 2016
    Messages:
    40
    Location:
    Sydney
    you can also do things like this if you only wanted a specific set of data
    Code:
    PS C:\Scripts (32) >  $PSObject | Where-Object{($_.Name -eq "JoeBlogs") -and ($_.OS -eq "Win 2008")} | Select-Object Type, Count
    
    Type Count
    ---- -----
    DC   5
    MS   10
    
    edit: expanded my laziness to proper commands instead of alias
     

Share This Page