How to generate a chart from PowerShell

334 views Asked by At

More specifically, I am trying to run something like get-mgdevicemanagementmanageddevice to get a chart of all devices from Intune and save as a .png file. I know how to do this. BUT, I also want to get the report for the past 4 days as well. I cannot seem to find any information on this. I know in PowerBi it collects this information from graph with the EventDateTimeUTC so I am sure this is possible. Any thoughts? I appreciate anyhelp!

I tried something like (Get-Date).AddDays(-1) but that doesnt do anything.

Please see my script below:

Connect-MgGraph

Add-Type -AssemblyName System.Windows.Forms

Add-Type -AssemblyName System.Windows.Forms.DataVisualization


# Import the required module

Import-Module Microsoft.PowerShell.Utility


#Variables for devices


$updateddevices = get-mgdevicemanagementmanageddevice -All | where {$_.OSVersion -eq '16.7'} | where {$_.DeviceCategoryDisplayName -eq 'in use'}

$totaldevices = get-mgdevicemanagementmanageddevice -all | where operatingsystem -eq 'iOS' |  where {$_.DeviceCategoryDisplayName -eq 'in use'}


# Define the data points

$dataPoints = @{

    "Updated" = $updateddevices.count

    "Total" = $totaldevices.count

   

}


# Create the chart

$chart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart


# Add the chart area

$chartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea

$chart.ChartAreas.Add($chartArea)


# Add a series to the chart

$series = New-Object System.Windows.Forms.DataVisualization.Charting.Series

$series.ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedColumn

$chart.Series.Add($series)


# Add data points to the series

$dataPoints.GetEnumerator() | ForEach-Object {

    $dataPoint = New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint 

    $dataPoint.AxisLabel = $_.Key

    $dataPoint.YValues[0] = $_.Value 

    $dataPoint.IsValueShownAsLabel = $true  # Display the data value on the chart

    $series.Points.Add($dataPoint)

}


# Set the chart title and axes labels

$chart.Titles.Add("iOS Compliant Devices")

$chartArea.AxisX.Title = "Devices"

$chartArea.AxisY.Title = "Count"


# Display the chart in a form

$form = New-Object System.Windows.Forms.Form

$form.Text = "Chart"

$form.ClientSize = New-Object System.Drawing.Size(500, 350)

$chart.Dock = [System.Windows.Forms.DockStyle]::Fill

$form.Controls.Add($chart)


# Show the form

$form.ShowDialog()


#Save Chart to png file

$chart.SaveImage("C:\Downloads\chart.png", 
[System.Drawing.Imaging.ImageFormat]::Png)"
0

There are 0 answers