Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The PowerShell CLI is used by jobs to create designer 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 add PivotTables and PivotCharts in the reports on Windows and Linux.

Find a sample report: jobscheduler_designed_report.xlsx


Code Block
languagexmlbash
titleCmdlets for ReportDesign
linenumberstrue
collapsetrue
<?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 = 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 = "/tmp/TaskHistory.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$WorkSheetName = "TaskHistory"
$excel = $data | Export-Excel $xlfile -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/jobscheduler_reporting.xls";
]]></script>
  <run_time/>
</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 11Line11: 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.
  • Line21Line 21:  Set the The location where excell Excell will be saved is set to a $xlfile variable.
  • Line 23Line23: Create a variable $WorkSheetName which is used to store the name of the worksheet.
  • Line24Line 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.
    • $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 the  -ClearSheet option to remove old data.
    • -PassThru returns the PivotTable so it can be customized.
    • -AutoSize the parameter  parameter allows us to resizes the columns of the spreadsheet to fit the data that is in it and to get the column-widths right
    • For adding the color to the conditional text  -ConditionalText is used. There are more types of conditional format are supported. Here we use conditional text for the job status color like (successful=green, Incomplete=range, Failed=Red)
    • For assigning the name to the worksheet -WorksheetName

       is used for naming the sheet

       parameter is used. The default name of the worksheet is sheet1.

    • Check for inserting a pivot table, so if -InsertPivotChart is specified, it implies -InsertPivotTableThe -IncludePivotTable and -IncludePivotChart cmdlets generate the pivot table and chart. the parameter -ChartType lets you pick what type of chart you want, there are many to choose from Examples: Area, Line, Pie, ColumnClustered, ColumnStacked100, ColumnClustered3D, ColumnStacked1003D, BarClustered, BarOfPie, Doughnut, etc. 
    • The -PivotRows and -PivotData parameters describe how to tabulate the data.
  • Line30Line 30PivotTableName parameter is used as the Name for the new PivotTable.
  • Line31Line 31: By default, a PivotTable will be created on its own sheet, but it can be created on an existing sheet.  $excelIn the above job example $excel.$WorkSheetName.cells["K1"] parameter defines the cell place in the existing worksheet, where the pivot table will be created.
  • Line32Line 32$excel.$WorkSheetName refers to the Worksheet where the source data is found.
  • Line33Line 33: PivotRows parameter is used for Fields to set as rows in the PivotTable.
  • Line34Line 34: 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.
  • Line35Line 35:  Apply For applying a table style to the PivotTablePivotTable 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 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. 

...