PowerShell Tip of the Week: Building your first chart report

Today’s short tip will be about building your first chart report. You can find several articles on our blog about memory utilization. This time you will learn how to get results in columns chart.

Chart report

As some of you already know PowerShell allows you to create charts and graphs. To do this we need to use .Net classes. The System.Windows.Forms.DataVisualization.Charting namespace contains methods and properties for the Chart Windows forms control. For more information about charting class please visit msdn site. As you can see below I use “StackedBar” chart for displaying memory report. You can check all available ChartTypes on msdn site.

Chart Memory
Chart Memory

Note: For purpose of this demonstration I just added “Server01” description for every column. Final output will be saved on user desktop.

#Saving PNG file on desktop
$Chart.SaveImage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")

Memory utilization report:
Script is using Get-WMIObject command to get memory information on remote machines.

$Servers = Get-Content -Path "C:\Users\$env:username\desktop\servers.txt"
Foreach($Server in $Servers)
{ 
    $Server = $Server.Trim()
    Write-Host "Processing $Server - " -ForegroundColor Green -NoNewLine
    $ComputerMemory =  Get-WmiObject -Class WIN32_OperatingSystem -ComputerName $Server
    $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)

    $Memory
}

Final script:

#Server list
$Servers = Get-Content -Path "C:\Users\$env:username\desktop\servers.txt"

[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms.DataVisualization')
 
$Chart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'

$ChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'Memory utilization'
$ChartArea.AxisY.Title = 'Percentage %'
$ChartArea.AxisX.Interval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')

#Define chart type - I use "StackedBar"
$Chart.Series['Memory'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar

#Looping servers
Foreach($Server in $Servers)
{ 
    $Server = $Server.Trim()
    Write-Host "Processing $Server" -ForegroundColor Green
    $ComputerMemory =  Get-WmiObject -Class WIN32_OperatingSystem -ComputerName $Server
    $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)

    If($Memory)
    { 
        $Value = $Chart.Series['Memory'].Points.AddXY("$Server","$Memory")
    }
}

$Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[0].Text = 'PowerShell Bros'

#Saving PNG file on desktop
$Chart.SaveImage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")

I hope that this was informative for you. See you in next articles 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.