Create a line chart with chart.js and powershell

Welcome Forums General PowerShell Q&A Create a line chart with chart.js and powershell

This topic contains 1 reply, has 2 voices, and was last updated by

 
Senior Moderator
2 months, 2 weeks ago.

  • Author
    Posts
  • #164121

    Participant
    Topics: 2
    Replies: 0
    Points: 11
    Rank: Member

    Hey everybody,
    i need help to create a line Chart with PowerShell. I have a pscostumobject:

    foreach ($item in $objectcollection['getEmailAppUsageUserCounts']) {
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Mail for Mac';
            'Count' = $item.'Mail for Mac';
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Outlook for Mac'
            'Count' = $item.'Outlook for Mac'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Outlook for Windows'
            'Count' = $item.'Outlook for Windows'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Outlook for Mobile'
            'Count' = $item.'Outlook for Mobile'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Other for Mobile'
            'Count' = $item.'Other for Mobile'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'Outlook for Web'
            'Count' = $item.'Outlook for Web'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'POP3 App'
            'Count' = $item.'POP3 App'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'IMAP4 App'
            'Count' = $item.'IMAP4 App'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
        $objR6 = [PSCustomObject]@{
            'Name'  = 'SMTP App'
            'Count' = $item.'SMTP App'
            'Date'  = $item.'Report Date'
        }
        $EmailAppUsageUserCountstable.add($objR6)
    }
    

    The $objectcollection i created with the following Code:

    $objectcollection = @{ }
    foreach ($report in $O365Reports) {
        $UsageData = Get-UsageReportData -ClientID $ClientID -ClientSecret $ClientSecret -TenantName $TenantName
        if ($UsageData) {
            $UsageData | add-member -membertype NoteProperty -name 'Office365Report' -Value $Report
            $objectCollection.Add($($Report), $UsageData)
        }
        else {
            $objResults = New-Object -Type PSObject
            $objResults | add-member -membertype NoteProperty -name 'Office365Report' -Value $Report
            $objectCollection.Add($($Report), $objResults)
        }
    }
    

    Now i will create a line Chart from the results. The result is:

    Name Count Date
    —- —– —-
    Mail for Mac 2019-06-02
    Outlook for Mac 2019-06-02
    Outlook for Windows 6 2019-06-02
    Outlook for Mobile 1 2019-06-02
    Other for Mobile 11 2019-06-02
    Outlook for Web 2019-06-02
    POP3 App 2019-06-02
    IMAP4 App 2019-06-02
    SMTP App 2019-06-02
    Mail for Mac 2019-06-03
    Outlook for Mac 2019-06-03
    Outlook for Windows 39 2019-06-03
    Outlook for Mobile 1 2019-06-03
    Other for Mobile 17 2019-06-03
    Outlook for Web 2019-06-03
    POP3 App 2019-06-03
    IMAP4 App 2019-06-03
    SMTP App 1 2019-06-03
    Mail for Mac 2019-06-04
    Outlook for Mac 2019-06-04
    Outlook for Windows 40 2019-06-04
    Outlook for Mobile 1 2019-06-04
    Other for Mobile 17 2019-06-04
    Outlook for Web 2019-06-04
    POP3 App 2019-06-04
    IMAP4 App 2019-06-04
    SMTP App 1 2019-06-04
    Mail for Mac 2019-06-05
    Outlook for Mac 2019-06-05
    Outlook for Windows 43 2019-06-05
    Outlook for Mobile 1 2019-06-05
    Other for Mobile 16 2019-06-05
    Outlook for Web 1 2019-06-05

    The Problem is now that i have multiple names with multiple Counts on different days. I would like to convertb now the result to following Schema:

    Mail for Mac

    2019-06-02 6
    2019-06-03 10
    and so on
    The same for the other names.

    Manually is the chart working but not in the script.
    Have anyone an idea?

  • #164223

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    You would use Group-Object cmdlet. You can use -Property Name, hence it will get grouped using the name property.

    Get-Help Group-Object -Online
    

You must be logged in to reply to this topic.