...
Starting Situation
- Instead of having the plain old text tabular visualization in Report report files, users can automatically design the Excel automate a more lively visual design to their Excel® reports by using the Import Excel Module.
- Users can organize all the Reports start as usual with data in rows and columns, however, with the selective choice of colors, PivotTablePivot Tables, and Charts can design the sheet for better visualization. PowerShell Excel module lets you create Excel pivot tables and charts from the raw dataa design is available that suggests better visualization and better decision making.
Use Cases
The PowerShell CLI is used by jobs to create designer 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 PivotTables and PivotCharts in the reports on Windows and LinuxPivot 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 for to your environment.
Download: report_design__task_history_windows_job.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?xml version="1.0" encoding="UTF-8" ?> <job title="Report Task History" process_class="agent_windows"> <script language="powershell"><![CDATA[ Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/ImportExcel; Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/JobScheduler; Connect-JS -Url $JOCCockpitUrl -Credential $JOCCockpitCredential | Out-Null; # Dates in local timezone, output includes local date format $data$reportData = Get-JSTaskHistory -Timezone (Get-Timezone ) ` | Select-Object -Property @{name="JobScheduler ID"; expression={$_.jobschedulerId}}, ` @{name="Task ID"; expression={$_.taskId}}, ` @{name="Job"; expression={$_.job}}, ` @{name="Status"; expression={$_.state._text}}, ` @{name="Start Time"; expression={ Get-Date $_.startTime }}, ` @{name="End Time"; expression={ Get-Date $_.endTime }}, ` @{name="Duration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").Seconds }}, ` @{name="Criticality"; expression={$_.criticality}}, ` @{name="Exit Code"; expression={$_.exitCode}} $xlfile$xlsxFile = "/tmp/TaskHistory.xlsx" Remove-Item $xlfile$xlsxFile -ErrorAction SilentlyContinue $WorkSheetName$workSheetName = "TaskHistory" $excel = $data$reportData | Export-Excel $xlfile$xlsxFile -ClearSheet -PassThru -AutoSize -AutoFilter -ConditionalText ` $( New-ConditionalText successful white green ` New-ConditionalText failed white Red ` New-ConditionalText Incomplete black orange ` ) -WorksheetName $WorkSheetName -IncludePivotTable -PivotRows "Job" -PivotColumn "Status" -PivotData @{"status"="count"} ` -IncludePivotChart -ChartType ColumnClustered3D $pivotTableParams = @{ PivotTableName = "ByJob" Address = $excel.$WorkSheetName.cells["K1"] SourceWorkSheet = $excel.$WorkSheetName PivotRows = @("JobScheduler ID", "Job", "Status") PivotData = @{'Status' = 'count'} PivotTableStyle = 'Medium6' } $pt = Add-PivotTable @pivotTableParams -PassThru $pt.RowHeaderCaption = "By " + ($pivotTableParams.PivotRows -join ",") Close-ExcelPackage $excel -Show Write-Output ".. report created: /tmp/TaskHistory.xlsxlsx"; ]]></script> <run_time/> </job> |
...
- 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 $data and $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 are stored in an object.
- Line 21: The location where Excell the Excel file will be saved is set to a
$xlfile
specified with the$xlsxFile
variable. - Line 23: Create a variable $WorkSheetName which
$workSheetName
that is used to store the name of the worksheet. - Line 24-27: creates 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$xlsxFile
stores the path of the Excel file where the Sourcesheet created.- If we are re-writing updating an existing sheet, worksheet and the new data doesn’t wouldn’t completely cover the oldarea 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 old 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 that is in it added and to get the column-widths right- For adding the color to the conditional text
-ConditionalText
is used. There are more Additional types of conditional format are supported. Here we use conditional text for the job status color like (successful=green, Incompleteincomplete=rangeorange, Failedfailed=Redred). To assign a For assigning the name to the worksheet the
-WorksheetName
parameter is used. The default name of the worksheet is sheet1.- The -
IncludePivotTable
and -IncludePivotChart
cmdlets parameters generate the pivot table and chart. 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 Examplesavailable 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 name for the new PivotTablePivot Table. - Line 31: By default, a PivotTable Pivot Table will be created on in its own sheetworksheet, but it can be created on in an existing sheetworksheet. In the above job example the
$excel.$WorkSheetName.cells["K1"]
parameter defines the cell place in the an existing worksheet , where the pivot table will be created. - Line 32:
$excel.$WorkSheetName
refers to the Worksheet where worksheet in which the source data is are found. - Line 33: PivotRows The
-PivotRows
parameter is used for Fields to set as rows in the PivotTable. - Line 34: PivotData The
-PivotData
parameter contains a hash table in of form "FieldName"="Function," where a function is one of Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP. - Line 35: For applying To apply a table style to the PivotTable 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 Charts:
- There is a sample PivotChart Pivot Chart created by with the use of parameter
-ChartType ColumnClustered3D
. The ColumnClustered3D ChartType chart type is used here in this example to show display the number of status jobs per state (successful, Incompleteincomplete, Failedfailed, Plannedplanned) of Jobsjobs.
Sample PivotTablePivot Table:
- The sample PivotTable shows 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.
...