Let’s make some charts in PowerShell, sometimes we will need to supply manager information for our environment, presentations or just for fun.

Here are examples of the charts you can get:

First we get the information into 2 arrays (we can use a hashtable too)

$Processes = Get-Process | Sort-Object -Property WS | Select-Object Name,PM -Last 5 $ProcessList = @(foreach($Proc in $Processes){$Proc.Name + "`n"+[math]::floor($Proc.PM/1MB)}) $Placeholder = @(foreach($Proc in $Processes){[math]::floor($Proc.PM/1MB)})

Now we can call the function bellow like this:

New-Chart -Title "Top processes" -List $ProcessList -Data $Placeholder

And get:

Or

New-Chart -Title "Top processes" -List $ProcessList -Data $Placeholder -Explode $false

And get:

Or

New-Chart -Title "Top processes" -List $ProcessList -Data $Placeholder -Pie $false -AxisYTitle "MB"

And get:

To get 7 biggest files under c:\windows: (using hashtable this time)

$files = dir c:\Windows -File | Sort-Object -Property Length | Select-Object Name,Length -Last 7 $info = @{} $files | %{ $info.add($_.Name,$_.Length) } New-Chart -Title "Biggest files under c:\windows" -List $info.keys -Data $info.values -Pie $false -AxisYTitle "Bytes"

And get:

To get a chart of the biggest mailboxes in Exchange 2010 for example use:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 $results = Get-Mailbox -ResultSize unlimited | Get-MailboxStatistics | Sort-Object -Property TotalItemSize | Select-Object displayName,TotalItemSize -Last 5 $info = @(foreach($mbx in $results){$mbx.displayName + "`n" + $mbx.TotalItemSize}) $data = @() $results | %{ $_.TotalItemSize -match "\((.+) bytes" $data += $Matches[1] } New-Chart -Title "Big Mailboxes" -List $info -Data $data

First you will need to Install Microsoft Chart Controls for Microsoft .NET Framework 3.5

Here is the function that will create pie or bar charts for you:

<# .SYNOPSIS Creates a chart and saves it to a .png file based on $List of information to display on the chart and corresponding $Data array of numeric values. .EXAMPLE $files = dir c:\Windows -File | Sort-Object -Property Length | Select-Object Name,Length -Last 7 $info = @{} $files | %{ $info.add($_.Name,$_.Length) } New-Chart -Title "Biggest files under c:\windows" -List $info.keys -Data $info.values -Pie $false -AxisYTitle "Bytes" #> function New-Chart{ param( [Parameter(Mandatory=$true)][array]$List, [Parameter(Mandatory=$true)][array]$Data, [string]$Title = " ", [Boolean]$Explode = $true, [int]$Width = 900, [int]$Height = 400, [Boolean]$Pie = $true, [string]$ImageFile = [Environment]::GetFolderPath("Desktop")+"\chart.png", #For use in bar charts [string]$AxisXTitle = " ", [string]$AxisYTitle = " " ) [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") $chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart $chart.Width = $Width $chart.Height = $Height $chart.BackColor = [System.Drawing.Color]::Transparent [void]$chart.Titles.Add($Title) $chart.Titles[0].Font = "ariel,48pt" $chart.Titles[0].Alignment = "topCenter" $chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea $chartarea.Name = "ChartArea1" $chart.ChartAreas.Add($chartarea) [void]$chart.Series.Add("data1") if($Pie) { $chart.Series["data1"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie $chart.Series["data1"].Points.DataBindXY($List, [int[]]$Data) $Chart.Series["data1"]["PieLabelStyle"] = "Outside" $Chart.Series["data1"]["PieLineColor"] = "Black" $Chart.Series["data1"]["PieDrawingStyle"] = "Concave" if($Explode) { ($Chart.Series["data1"].Points.FindMaxByValue())["Exploded"] = $true } } else { $chartarea.AxisX.Interval = 1 $ChartArea.AxisX.LabelStyle.Font = "ariel,10pt" $ChartArea.AxisY.LabelStyle.Font = "ariel,10pt" $ChartArea.AxisX.Title = $AxisXTitle $ChartArea.AxisY.Title = $AxisYTitle $chart.Series["data1"].Points.DataBindXY($List, [int[]]$Data) $maxValue = $Chart.Series["data1"].Points.FindMaxByValue() $maxValue.Color = [System.Drawing.Color]::Red $minValue = $Chart.Series["data1"].Points.FindMinByValue() $minValue.Color = [System.Drawing.Color]::Green $Chart.Series["data1"]["DrawingStyle"] = "Cylinder" } $chart.SaveImage($ImageFile,"png") }

Share this: Twitter

Facebook

Like this: Like Loading... Related

Tagged: Chart, Get-Mailbox, Get-MailboxStatistics, hashtable, Select-Object, Sort-Object