Versions Compared

Key

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

...

Code Block
languagebash
titleShebang for PowerShell Job with Windows
@@setlocal enabledelayedexpansion & set f=%RANDOM%.ps1 & @@findstr/v "^@@f^@@[fs].*&" "%~f0"|pwsh > !f! & powershell.exe -&goto:eofNonInteractive -File !f! & set e=!errorlevel! & del /q !f! & exit !e!/b&

Explanation:

  • Credits for the Windows shebang replacement go to How to run a PowerShell script within a Windows batch file.
  • If you consider this shebang replacement somewhat cryptic then add it to JS7 - Script Includes which are easily referenced from a shell job, for example by using ##!include pwsh.
  • The PowerShell executable pwsh.exe is available starting from PowerShell 6.0. PowerShell releases 5.x use the executable powershell.exe which can be specified accordingly with the shebang.

...

Code Block
languagepowershell
titleOrder History Report Job
linenumberstrue
@@setlocal enabledelayedexpansion & set f=%RANDOM%.ps1 & @@findstr/v "^@@f^@@[fs].*&" "%~f0"|pwsh > !f! & powershell.exe -&goto:eofNonInteractive -File !f! & set e=!errorlevel! & del /q !f! & exit !e!/b&

Import-Module ImportExcel
Import-Module JS7

$credentials = ( New-Object -typename System.Management.Automation.PSCredential -ArgumentList 'root', ( 'root' | ConvertTo-SecureString -AsPlainText -Force) )
Connect-JS7 -Url $env:JS7_JOC_URL -Credentials $credentials -Id $env:JS7_CONTROLLER_ID | Out-Null

# Dates in local time zone, output includes local date format
Get-JSOrderHistory -Timezone (Get-Timezone) -RelativeDateFrom -3d `
                |  Select-Object -Property @{name="Controller ID"; expression={$_.controllerId}}, `
                                           @{name="History ID"; expression={$_.historyId}}, `
                                           @{name="Order ID"; expression={$_.orderId}}, `
                                           @{name="Order Status"; expression={$_.state._text}}, `
                                           @{name="Order Position"; expression={$_.position}}, `
                                           @{name="Workflow"; expression={$_.workflow}}, `
                                           @{name="History Status"; expression={$_.state._text}}, `
                                           @{name="Planned Start Time"; expression={ Get-Date $_.plannedTime }}, `
                                           @{name="Start Time"; expression={Get-Date $_.startTime}}, `
                                           @{name="End Time"; expression={ Get-Date $_.endTime }}, `
                                           @{name="Duration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").TotalSeconds }} `
                | Export-Excel -Path /tmp/jobscheduler_reporting.xlsx -WorksheetName "Order-History" -ClearSheet

Write-Output ".. report created: /tmp/jobscheduler_reporting.xlsx"				

...

  • Line 1: The job is executed with a Windows Agent and makes use of the PowerShell shebang for Windows, as explained above.
  • Line 3-4: The required PowerShell modules are imported. They could be installed in any location in the file system
  • Line 6-7: The Connect-JS7 cmdlet is used to authenticate with the JS7 REST Web Service API. The arguments required for -Url , -Credentials and -Id can specified in a number of ways:
  • Line 10: The Get-JS7OrderHistory cmdlet is invoked:
    • with the -Timezone parameter to specify which time zone the date values in the report should be converted to. The -Timezone (Get-Timezone) parameter value specifies that the time zone of the Agent's server is used. Otherwise specify the desired time zone, for example like this: -Timezone (Get-Timezone -Id 'GMT Standard Time'). Without using this parameter any date values are stored in the report as UTC dates.
    • with additional optional parameters, for example to specify the date or date range which the report is created for. A value -RelativeDateTo -3d specifies that the report should cover the last 3 days (until midnight). Keep in mind that dates have to be specified for the UTC time zone. Without this parameter the report will be created for the next day.
    • see the Get-JS7OrderHistory cmdlet cmdlet for a the full parameter reference.
  • Line 11-21: From the output of the Get-JS7OrderHistory cmdlet a number of properties are selected and are specified for the sequence in which they should occur in the report. 
    • To add more appropriate column headers the property names are mapped to a more readable textual representation.
    • Note the handling of date formats in lines 17-21. Use of the Get-Date cmdlet converts the output format of dates (not the time zone) to the default format which is in place on the Agent's server. Without using the Get-Date cmdlet any date values will be stored in the report in ISO format, e.g. 2020-12-31 10:11:12+02:00 for a date in the European central time zone that is UTC+1 in winter time and UTC+2 in summer time.
    • Line 21 introduces a new property, a calculated duration. From the start time and end time values of a planned start the difference in seconds is calculated and added to the report.
  • Line 22: The list of properties per Order History item is piped to the Export-Excel cmdlet which is available with the ImportExcel PowerShell Module. The report file name is specified and optionally the worksheet. For a full list of parameters see the ImportExcel PowerShell Module article.


Code Block
languagepowershell
titleOrder History Report Job for a single Workflow
linenumberstrue
# Use the -WorkflowPath argument to specify the path or name of a workflow
Get-JSOrderHistory -WorkflowPath pdwScheduledWorkflow_001 -Timezone (Get-Timezone) -RelativeDateFrom -3d `
                |  Select-Object -Property @{name="Controller ID"; expression={$_.controllerId}}, `
                                           @{name="History ID"; expression={$_.historyId}}, `
                                           @{name="Order ID"; expression={$_.orderId}}, `
                                           @{name="Order Status"; expression={$_.state._text}}, `
                                           @{name="Order Position"; expression={$_.position}}, `
                                           @{name="Workflow"; expression={$_.workflow}}, `
                                           @{name="History Status"; expression={$_.state._text}}, `
                                           @{name="Planned Start Time"; expression={ Get-Date $_.plannedTime }}, `
                                           @{name="Start Time"; expression={Get-Date $_.startTime}}, `
                                           @{name="End Time"; expression={ Get-Date $_.endTime }}, `
                                           @{name="Duration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").TotalSeconds }} `
                | Export-Excel -Path /tmp/jobscheduler_reporting.xlsx -WorksheetName "Order-History" -ClearSheet

Write-Output ".. report created: /tmp/jobscheduler_reporting.xlsx"				



Code Block
languagepowershell
titleOrder History Report Job for selected Workflows
linenumberstrue
# Create an array of objects holding the workflowPath property and the path or name of related workflow
$workflows = @()

$workflow = New-Object PSObject
Add-Member -Membertype NoteProperty -Name 'workflowPath' -value "pdwScheduledWorkflow_001" -InputObject $workflow
$workflows += $workflow

$workflow = New-Object PSObject
Add-Member -Membertype NoteProperty -Name 'workflowPath' -value "pdwScheduledWorkflow_002" -InputObject $workflow
$workflows += $workflow

$workflow = New-Object PSObject
Add-Member -Membertype NoteProperty -Name 'workflowPath' -value "pdwScheduledWorkflow_003" -InputObject $workflow
$workflows += $workflow

# Pipe the arry of objects to the cmdlet
$workflows | Get-JSOrderHistory -Timezone (Get-Timezone) -RelativeDateFrom -3d `
                |  Select-Object -Property @{name="Controller ID"; expression={$_.controllerId}}, `
                                           @{name="History ID"; expression={$_.historyId}}, `
                                           @{name="Order ID"; expression={$_.orderId}}, `
                                           @{name="Order Status"; expression={$_.state._text}}, `
                                           @{name="Order Position"; expression={$_.position}}, `
                                           @{name="Workflow"; expression={$_.workflow}}, `
                                           @{name="History Status"; expression={$_.state._text}}, `
                                           @{name="Planned Start Time"; expression={ Get-Date $_.plannedTime }}, `
                                           @{name="Start Time"; expression={Get-Date $_.startTime}}, `
                                           @{name="End Time"; expression={ Get-Date $_.endTime }}, `
                                           @{name="Duration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").TotalSeconds }} `
                | Export-Excel -Path /tmp/jobscheduler_reporting.xlsx -WorksheetName "Order-History" -ClearSheet

Write-Output ".. report created: /tmp/jobscheduler_reporting.xlsx"