Versions Compared

Key

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

Table of Contents

Introduction

  • PowerShell® is a frequently used scripting language available for Linux, MacOS, Windows and other platforms.
  • JS7 offers the JS7 - PowerShell Module for simplified access to the JS7 - REST Web Service API
  • This article explains how to syntactically include PowerShell® scripts with JS7 job scripts

...

  • Find the below examples for download (.json upload)run-powershell-unixpdRunPowerShell4Unix.workflow.json
  • In order to directly add run PowerShell® script code to from a JS7 shell job script the recommended approach is to use a shebang like this:

    Code Block
    languagebash
    titleExample how use PowerShell run PowerShell® script code with a shebang
    linenumberstrue
    #!/usr/bin/env pwsh
     
    Write-Output "Hello"
    Write-Output "world"
  • As a bad alternative the PowerShell® executable can be invoked directly and can be parameterized like this

    Code Block
    languagebash
    titleExample how to add PowerShell run PowerShell® script code from a single line
    linenumberstrue
    pwsh -NoLogo -NonInteractive -Command '& { Echoecho "Hello"; Echoecho "World"; }'

    Explanation:

    • Consider the quoting: when using the -Command parameter then the PowerShell® script has to be specified from a string. This includes to :
      • quote quoting the script by single quotes.
      • quote quoting code inside the script with double quotes or to use using escape characters for single quotes.
    • Consider Note that each PowerShell® command has to be terminated with a semicolon.

  • An even more weird way how to add of running PowerShell® code to from JS7 job scripts includes:

    Code Block
    languagebash
    titleExample how to add PowerShell run PowerShell® script code from a multiple linesnumber of lines
    linenumberstrue
    pwsh -NoLogo -NonInteractive -Command '& { `
      Echo "Hello"; `
      Echo "World"; `
    }'

    Explanation:

    • Consider Note the use of single quotes, double quotes and semicolons as from in the previous example. 
    • In addition each line of script code has to be terminated with a backtick for line continuation.
  • Last but not least In addition, a PowerShell® script can be executed from a file that has to be located with the respective is in reach of the JS7 Agent:

    Code Block
    languagebash
    titleExample how to add PowerShell run PowerShell® script code from a file
    linenumberstrue
    pwsh -NoLogo -NonInteractive -File some_powershell_script.ps1
  • For any above ways how to call PowerShell® consider to activate the setting Fail on output to stderr that is available from the Job Options tab of a job's properties in the Configuration view.: PowerShell is far from perfect in reporting exit codes, for example when exceptions are raised then this will not return a non-zero exit code to the Windows shell calling PowerShell®. Instead a job's output to the stderr channel has to be checked to make an order fail in case of PowerShell® exceptions.

Windows

  • Find the below examples for download (.json upload)run-powershell-windowspdRunPowerShell4Windows.workflow.json
  • In order to directly add run PowerShell® script code to from a JS7 shell job script the recommended approach is to use a shebang replacement like this:

    Code Block
    languagebash
    titleExample how use PowerShell run PowerShell® script code with a shebang replacement
    linenumberstrue
    @@setlocal enabledelayedexpansion & @@findstr/v "^@@f^@@[fs].*&" "%~f0" |pwsh powershell.exe -&goto:eofNonInteractive -Command - & exit !errorlevel!/b&
    
    Write-Output "Hello" 
    Write-Output "world" 

    Explanation:

    Credits for the shebang replacement 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 that which are easily referenced from a shell jobjobs, e.g. by using ##!include pwsh
    • The PowerShell executable ® pwsh.exeexe executable is available starting from with PowerShell 6.0. PowerShell releases 5.x use the executable powershell.exe that that can be used accordingly with the shebang accordingly. 

      Therefore if using a version > 5.1 please change powershell.exe to pwsh.exe:

      @@setlocal enabledelayedexpansion & @@findstr/v "^@@[fs].*&" "%~f0" | pwsh.exe -NonInteractive -Command - & exit !errorlevel!/b&

  • As a bad alternative the PowerShell® executable can be invoked directly and can be parameterized like this:

    Code Block
    languagebash
    titleExample how to add PowerShell run PowerShell® script code from a single line
    linenumberstrue
    pwsh.exe -NoLogo -NonInteractive -Command "& { Echo ""Flight Destination: $env:FLIGHT_DESTINATION""; Echo ""Booking Code: $env:BOOKING_CODE""; }"

    Explanation:

    • Consider the quoting: when using the -Command parameter then the PowerShell® script has to be specified from a string. This includes to :
      • quote quoting the script by double quotes.
      • quote code quoting code inside the script with two double quotes or to use using single quotes.
    • Consider Note that each PowerShell® command has to be terminated with a semicolon.


  • Last but not least In addition, a PowerShell® script can be executed from a file that has to be located with the respective is located in reach of the JS7 Agent:

    Code Block
    languagebash
    titleExample how to add PowerShell run PowerShell® script code from a file
    linenumberstrue
    pwsh.exe -NoLogo -NonInteractive -File some_powershell_script.ps1
  • For any above ways how to call PowerShell® consider to activate the setting Fail on output to stderr that is available from the Job Options tab of a job's properties in the Configuration view.: PowerShell is far from perfect in reporting exit codes, for example when exceptions are raised then this will not return a non-zero exit code to the Windows shell calling PowerShell®. Instead a job's output to the stderr channel has to be checked to make an order fail in case of PowerShell® exceptions.