You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »


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




Cmdlets for ReportDesign
$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 = "$env:tmp\TaskHistory.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$WorkSheetName = "TaskHistory"
$excel = $data | Export-Excel $xlfile -ClearSheet -PassThru -AutoSize -AutoFilter -TableName ByJobStatus -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


Explanation: 

  • Line1: 
  • 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: creates a spreadsheet, and pass on the Excel Package object which provides the reference to the workbook and turns to the worksheets inside it
  • 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"
  • No labels