Versions Compared

Key

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

...

  • JobScheduler supports PowerShell as a language for implementation of jobs, see 
    Jira
    serverSOS JIRA
    columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
    serverId6dc67751-9d67-34cd-985b-194a8cdc9602
    keyJS-1595
  • The support for PowerShell includes API Shell jobs and shell API jobs:.
    • PowerShell jobs can use any Windows commands or PowerShell commandscmdlets and functions.
    • PowerShell jobs can use the objects and methods of the JobScheduler API.
  • There are some Some minor differences exist between Shell jobs and PowerShell jobs, see Compatibility between PowerShell as a and Shell.
  • PowerShell jobs are executed with Agents, not with a JobScheduler Master.

...

Example 1: A simple PowerShell job with some scripting similar to basic shell jobs might look like this:

...

Code Block
<?xml version="1.0" encoding="ISO-8859-1"?>
<job  process_class="/tests/Agent" stop_on_error="no">
    <script  language="powershell">
        <![CDATA[
function spooler_process()
{		
	# set variables to store the information about job and task 	
	$jobName = $spooler_job.name()
	$jobTitle = $spooler_job.title()
	$taskId = $spooler_task.id()
	
	# display the information about job and task
	echo "job $jobName with title $jobTitle is starting"
	echo "task ID is $taskId"
		
	echo "job is finishing"
		
	# for standalone jobs set return code to false / for order jobs set to true in case of successful execution
	return $false
}
        ]]>
    </script>
    <run_time />
</job>

...

This PowerShell job implements a  pre-processing Monitor script (API job) that is executed before the job script is executed:

Code Block
<?xml version="1.0" encoding="ISO-8859-1"?>
<job  process_class="my_Agent">

    <script  language="powershell">
        <![CDATA[
echo "job is starting"
Start-Sleep -Seconds 10
echo "job is finishing"
        ]]>
    </script>

    <monitor  name="process_powershell" ordering="0">
        <script  language="powershell">
            <![CDATA[
function spooler_process_before()
{
	# check for a "go" file that is required to start the job
	$rc = ( Test-Path -Path "/tmp/go.txt" -PathType Leaf )
	echo ".. looking up go file: $rc"
	return $rc
}
            ]]>
        </script>
    </monitor>

    <run_time />
</job>

...

The following job explains how to use different output channels for PowerShell such as:

 

Code Block
<?xml version="1.0" encoding="ISO-8859-1"?>
<job  process_class="my_Agent" stop_on_error="no">
    <settings >
        <log_level ><![CDATA[debug1]]></log_level>
    </settings>

    <script  language="powershell">
        <![CDATA[
# Use Write-Output or Echo cmdlets to write to the JobScheduler log
Write-Output "job: this is some output"
echo "job: this is some output"

# This does not work: Use of Write-Host cmdlet is not applicable
# Write-Host "job: this is some output"

# Standard PowerShell verbose setting is considered for log output
$VerbosePreference = "Continue"
Write-Verbose "job: this is some verbose output"

# Standard PowerShell debug setting is considered for log output
$DebugPreference = "continueContinue"
# In addition the current log level of the job has to be set, e.g. log level "debug1" logs debug messages
Write-Debug "job: this is some debug output"

# creates a warning for the job
Write-Warning "job: this is a warning"

# can be used to throw an error
# Write-Error "job: this is an error"
        ]]>
    </script>

    <run_time />
</job>

Explanations

  1. Using PowerShell standard output
    • Use of the Write-Output and Echo cmdlets is applicable.
    • Use of the Write-Host cmdlet is not applicable for PowerShell jobs as the cmdlet requires a PowerShell host console to be available (powershell.exe), whereas JobScheduler runs PowerShell in a process without interaction.
  2. Using PowerShell verbose output
    • The standard PowerShell verbosity setting is considered for log output
    • Use $VerbosePreference = "Continue" 
    • Subsequently use the Write-Verbose cmdlet.
  3. Using PowerShell debug messages
    • The PowerShell debug setting is considered for log output in jobs by use of $DebugPreference = "Continue"
    • In addition, the current log level of the job has to be set, e.g. log level debug1 will log debug messages.
      • With the JobScheduler log level being switched to info no debug output is written to the log file.
      • With the JobScheduler log level being switched to debug1debug2, ..., debug9 then debug output is added to the log.
  4. Using PowerShell Warnings
    • Warnings are created by use of the Write-Warning cmdlet. Such warnings create corresponding warnings in the JobScheduler Master that are visible from the log and that might trigger a notification by mail.
  5. Using PowerShell Error Messages
    • Use of the Write-Error cmdlet will create a job error that is visible from the log and that triggers subsequent actions as e.g. notification by mail, stopping the job, suspending an order etc. 

...

Info
  • The feature is only available in case your Agent is running on a machine with PowerShell available
  • For more information about how to install and to use the JobScheduler PowerShell CLI module for an Agent see PowerShell Command Line Interface - Introduction

...

The JobScheduler PowerShell CLI module is available for PowerShell jobs. A basic job using the PowerShell CLI might look like this:

...

  • The PowerShell CLI is activated by the Import-Module JobScheduler command.
  • For a complete list of cmdlets available from the PowerShell CLI see PowerShell CLI - Cmdlets

Additional examples are available from the PowerShell CLI - Use Cases article.

Anchor
powershell-as-a-shell
powershell-as-a-shell
Compatibility between PowerShell and Shell

PowerShell jobs can be considered as a migration path for shell jobs. This suggests that every any shell job can possibly be converted (with a few changes) to a PowerShell job. 

...

Calling parameters works differently for PowerShell jobs than for shell jobs, using $env:VARIABLE for PowerShell instead of %VARIABLE% as for shell:

Code Block
# Example Shell: 
myscript.cmd %SCHEDULER_PARAM_NAME1%

# Example PowerShell: 
myscript.cmd $env:SCHEDULER_PARAM_NAME1

...

Change Management References

...