Skip to end of metadata
Go to start of metadata

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

Unix

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

    Example how use PowerShell script code with a shebang
    #!/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

    Example how to add PowerShell script code from a single line
    pwsh -NoLogo -NonInteractive -Command '& { Echo "Hello"; Echo "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 the script by single quotes.
      • quote code inside the script with double quotes or to use escape characters for single quotes.
    • Consider that each PowerShell command has to be terminated with a semicolon.

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

    Example how to add PowerShell script code from a multiple lines
    pwsh -NoLogo -NonInteractive -Command '& { `
      Echo "Hello"; `
      Echo "World"; `
    }'

    Explanation:

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

    Example how to add PowerShell script code from a file
    pwsh -NoLogo -NonInteractive -File some_powershell_script.ps1

Windows

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

    Example how use PowerShell script code with a shebang replacement
    @@findstr/v "^@@f.*&" "%~f0"|pwsh.exe -&goto:eof
    
    Write-Output "Hello" 
    Write-Output "world" 

    Explanation:

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

    Example how to add PowerShell script code from a single line
    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 the script by double quotes.
      • quote code inside the script with two double quotes or to use single quotes.
    • Consider that each PowerShell command has to be terminated with a semicolon.


  • Last but not least a PowerShell script can be executed from a file that has to be located with the respective Agent:

    Example how to add PowerShell script code from a file
    pwsh.exe -NoLogo -NonInteractive -File some_powershell_script.ps1



  • No labels
Write a comment…