Starting Situation
- Instead of having the plain old text in Report files, users can automatically design the Excel reports by using the Import Excel Module.
- Users can organize all the data in rows and columns, with the selective choice of colors, PivotTable, and Charts can design the sheet for better visualization. PowerShell Excel module lets you create Excel pivot tables and charts from the raw data.
Use Cases
The PowerShell CLI is used by jobs to create reports. Two modules are applied for this purpose:
- the JobScheduler PowerShell Module
- a reporting PowerShell Module. This example makes use of the ImportExcel PowerShell Module that can be used to create Excel® reports on Windows and Linux.
Find a sample report: jobscheduler_designed_report.xlsx
cmdlets
Explanation:
- Line1: Create an object $data and the output of the Get-JSTaskHistory cmdlet in which a number of properties are selected and are specified for the sequence in which they should occur in the report are stored in an object.
- Line11: Set the location where excell will be saved
- Line13: Create a variable $WorkSheetName which is used to store the name of the worksheet.
- Line14-16: creates a spreadsheet, and passes on the Excel Package object which provides the reference to the workbook and turns to the worksheets inside it.
- $xlfile store the path of the file where the Sourcesheet created.
- Using -ClearSheet to remove old data. If we are re-writing an existing sheet, and the new data doesn’t completely cover the old, we may be left with “ghost” data. To ensure this doesn’t happen, we can use the ‑ClearSheet option
the parameter which removes the worksheet and any past data. - -PassThru returns the PivotTable so it can be customized.
- Line20: PivotTableName parameter is used as the Name for the new PivotTable.
- Line21: By default, a PivotTable will be created on its own sheet, but it can be created on an existing sheet. $excel.$WorkSheetName.cells["K1"] defines the cell place in the existing worksheet, where the pivot table will be created.
- Line22: $excel.$WorkSheetName refers to the Worksheet where the source data is found.
- Line23: PivotRows parameter is used for Fields to set as rows in the PivotTable.
- Line24: PivotData parameter contains a hash table in form "FieldName"="Function," where a function is one of Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP.
- Line25: Apply a table style to the PivotTable. The PivotTableStyle “Medium6” is the default table style but there are plenty of others to choose from. Example: PivotTableStyles = None, Custom, Light1 to Light21, Medium1 to Medium28, Dark1 to Dark11".
Sample Charts:
- There is a sample PivotChart created by the use of parameter -ChartType ColumnClustered3D. The ColumnClustered3D ChartType is used here to show the number of status (successful, Incomplete, Failed, Planned) of Jobs.