Starting Situation
- Instead of the plain old tabular visualization in report files, users can automate a more lively visual design to their Excel® reports by using the Import Excel Module.
- Reports start as usual with data in rows and columns, however, with the choice of colors, Pivot Tables, and Charts a design is available that suggests better visualization and better decision making.
Use Cases
The PowerShell CLI is used by jobs to create better designed 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 and to add Pivot Tables and Pivot Charts to reports. The module can be operated for Windows and Linux computers and does not require prior installation of Microsoft Excel®.
Find a sample report: jobscheduler_designed_report.xlsx
Please consider that below jobs are examples that have to be adjusted to your environment.
Download: report_design__task_history_windows_job.
Explanation:
- Line 2-3: The job is executed with a Windows Agent that is assigned by a process class. The job is of type "Powershell" and will use the Powershell version provided with the server.
- Line 4-5: The required PowerShell modules are imported. They could be installed with any location in the file system
- Line 7: The Connect-JS cmdlet is used to authenticate with the JOC Cockpit REST Web Service. The required URL and credentials are specified in a PowerShell profile, see PowerShell CLI 1.2 - Use Cases - Credentials Management
- Line 11: Create an object $reportData from 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.
- Line 21: The location where the Excel file will be saved is specified with the
$xlsxFile
variable. - Line 23: Create a variable
$workSheetName
that is used to store the name of the worksheet. - Line 24-27: Creates a spreadsheet and passes on the Excel Package object which provides the reference to the workbook and turns to the worksheets inside it.
$xlsxFile
stores the path of the Excel file.- If we are updating an existing worksheet and the new data wouldn’t completely cover the area consumed by previous data, then we may be left with “ghost” data. To ensure this doesn’t happen, we can use the
-ClearSheet
option to remove previous data in a worksheet. -PassThru
returns the PivotTable so it can be customized.-AutoSize
parameter allows us to resizes the columns of the spreadsheet to fit the data added and to get the column-widths right- For adding the color to the conditional text
-ConditionalText
is used. Additional types of conditional format are supported. Here we use conditional text for the job status color like (successful=green, incomplete=orange, failed=red). To assign a name to the worksheet the
-WorksheetName
parameter is used. The default name of the worksheet is sheet1.- The -
IncludePivotTable
and -IncludePivotChart
parameters generate the Pivot Table and Pivot Chart. The parameter -ChartType
lets you pick what type of chart you want to use, there are many to choose from available examples: Area, Line, Pie, ColumnClustered, ColumnStacked100, ColumnClustered3D, ColumnStacked1003D, BarClustered, BarOfPie, Doughnut, etc. - The -
PivotRows
and-
PivotData
parameters describe how to tabulate the data.
- Line 30: The
-PivotTableName
parameter is used as the name for the new Pivot Table. - Line 31: By default, a Pivot Table will be created in its own worksheet, but it can be created in an existing worksheet. In the above job example the
$excel.$WorkSheetName.cells["K1"]
parameter defines the cell place in an existing worksheet where the pivot table will be created. - Line 32:
$excel.$WorkSheetName
refers to the worksheet in which the source data are found. - Line 33: The
-PivotRows
parameter is used for Fields to set as rows in the PivotTable. - Line 34: The
-PivotData
parameter contains a hash table of form "FieldName"="Function," where a function is one of Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP. - Line 35: To apply a table style to the Pivot Table the
-PivotTableStyle
parameter is used. 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 Report Sheet with colorful status:
The below screenshot contains the output of the Get-JSTaskHistory stored in the Excel sheet report. Users can change the colors of text and background of cells according to their choice. The below example represents the status of Jobs with different colors using
-ConditionalText
parameter of the Import Excel module.
Sample Charts:
- There is a sample Pivot Chart created with the parameter
-ChartType ColumnClustered3D
. The ColumnClustered3D chart type is used in this example to display the number of jobs per state (successful, incomplete, failed, planned) of jobs.
Sample Pivot Table:
- The sample Pivot Table displays the RowHeaderCaption as "By JobScheduler ID, Job, Status" which is also present in PivotTable to represent the status of Jobs according to JobScheduler ID. You can adjust the style of the table by the parameter
PivotTableStyle
. Users can also change the view like expand and collapse the view of the table.