(How to create reports based on CSV file with powershell and .NET libraries)
In this video I demonstrate how you can create reports based on excel input. This video s an extension on another video about creating beautifull reports with powershell.
*powershell
*learn powershell
*create reports
*pyramid chart
*doughnut chart
Code :
#region normal functions
#function for save to PNG file, not needed, only if you click the save button its called
Function Invoke-SaveDialog {
$FileTypes = [enum]::GetNames('System.Windows.Forms.DataVisualization.Charting.ChartImageFormat')| ForEach {
$_.Insert(0,'*.')
}
$SaveFileDlg = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDlg.DefaultExt='PNG'
$SaveFileDlg.Filter=";Image Files ($($FileTypes))|$($FileTypes)|All Files (*.*)|*.*";
$return = $SaveFileDlg.ShowDialog()
If ($Return -eq 'OK') {
[pscustomobject]@{
FileName = $SaveFileDlg.FileName
Extension = $SaveFileDlg.FileName -replace '.*\.(.*)','$1'
}
}
}
function ItemsToHash($items){
$hash = @{}
foreach ($item in $items){
$hash.Add([string]($item.name), $item.Count)
}
return $hash
}
#endregion
#region special functions
function displaychart {
param(
[Parameter(Mandatory)][hashtable]$items,
[Parameter(Mandatory)][string]$title,
[Parameter(Mandatory)][ValidateSet("doughnut","pyramid")][string]$chart_type
)
#we need keys and values here
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Series = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series
$ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]
$Series.ChartType = $ChartTypes::$chart_type #play with this value to change the looks
$Chart.Series.Add($Series)
$Chart.ChartAreas.Add($ChartArea)
#HERE YOU ADD YOUR DATA TO THE CHART!!!!!!!!!!!!!!!
$Chart.Series['Series1'].Points.DataBindXY($items.Keys, $items.Values)
#$Chart.Series['Series1'].Points.DataBindXY($Processes.Name, $Processes.WS)
$Chart.Width = 700
$Chart.Height = 400
$Chart.Left = 10
$Chart.Top = 10
$Chart.BackColor = [System.Drawing.Color]::LightGray
$Chart.BorderColor = [System.Drawing.Color]::BlueViolet
#$Chart.BorderColor = 'Blue'
$Chart.BorderWidth = 3
$Chart.BorderDashStyle = 'Solid'
$ChartTitle = New-Object System.Windows.Forms.DataVisualization.Charting.Title
$ChartTitle.Text = $title
$Font = New-Object System.Drawing.Font @('Microsoft Sans Serif','12', [System.Drawing.FontStyle]::Italic)
$ChartTitle.Font = $Font
$ChartTitle.ForeColor = [System.Drawing.Color]::Black
$Chart.Titles.Add($ChartTitle)
$Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$Legend.IsEquallySpacedItems = $True
$Legend.BorderColor = 'Green'
$Legend.BorderWidth = 2
$Chart.Legends.Add($Legend)
$Chart.Series['Series1']['PieLineColor'] = 'Black'
$Chart.Series['Series1']['PieLabelStyle'] = 'Outside'
$Chart.Series['Series1'].Label = "#VALX (#VALY)"
#region Windows Form to Display Chart
$AnchorAll = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Width = 740
$Form.Height = 490
$Form.controls.add($Chart)
$Chart.Anchor = $AnchorAll
#endregion
#region save button
$SaveButton = New-Object Windows.Forms.Button
$SaveButton.Text = "Save"
$SaveButton.Top = 420
$SaveButton.Left = 600
$SaveButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
[enum]::GetNames('System.Windows.Forms.DataVisualization.Charting.ChartImageFormat')
$SaveButton.add_click({
$Result = Invoke-SaveDialog
If ($Result) {
$Chart.SaveImage($Result.FileName, $Result.Extension)
}
})
#endregion
#region display form and add save button to form control
$Form.controls.add($SaveButton)
$Form.Add_Shown({$Form.Activate()})
[void]$Form.ShowDialog()
#endregion
}
#endregion
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
$CWD = Split-Path $MyInvocation.MyCommand.Definition
$countries = import-csv "$CWD\testest.csv"
#Doughnut based on CSV
$title = "Country Dougnut"
$newvalues2 = ItemsToHash -items $countries
displaychart -items $newvalues2 -title $title -chart_type doughnut
#Pyramid based on CSV
$title = "Country Pyramid"
$newvalues3 = ItemsToHash -items $countries
displaychart -items $newvalues3 -title $title -chart_type pyramid
Смотрите видео PowerShell - Charting онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Mr Automation 30 Март 2021. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 617 раз и оно понравилось 15 посетителям.