Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Conversion corrections

Table of Contents
outlinh1. true
outlinh1. true
1printablefalse
2stylh1. none
3indent20px

There are at least two different approaches to pass parameters to a ps-script:

...

As an example for both methods we will use a simple script which will list the content of a folder, filtered by a given filename extension. The parameters are defines in an order like this:

Code Block

 <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/order.xml order] <order title="Executes the script ListFilesByExtension.ps1">
    <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/params.xml params]><params>
        <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/param.xml param ]<param name="Script_Filename"    value="ListFilesByExtension.ps1"/>
        <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/param.xml param ]<param name="FolderName"         value="c:\temp"/>
        <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/param.xml param ]<param name="FileNameExtension"  value=".txt"/> 
    </params>
    <[http://www.sos-berlin.com/doc/en/scheduler.doc/xml/run_time.xml run_time ]<run_time let_run="no"/>
 </order>

'FolderName' ist the name of the folder which has to be listed and 'FileNameExtension' is the value of the extension which will be selected.

The script code, without the initialisation of the parameters, is shown below:

Code Block

 $Dir = .....
 $Ext = .....
 
 $a = "List $Dir with Extension $Ext `n" + "==========================";
 $a
 
 # Filenames as a list
 GCI $Dir -R | Where \{$_.Extension -EQ $Ext\} | sort-Object -descending Length | Format-List -property * 

...

using position parameters

Code Block

  powershell ... -file "%SCHEDULER_PARAM_SCRIPT_FILENAME%" "%SCHEDULER_PARAM_FolderName%" "%SCHEDULER_PARAM_FileNameExtension%"
Code Block

 $Dir = $args[0]
 $Ext = $args[1]
 .
 ...

...

Starting a script like below:

Code Block

    .\CreateVariablesFromEnvironment -Prefix "SCHEDULER_PARAM_"

and the first statement in the script is the param-statement:

Code Block

 param ([string] $Prefix)

will fill the variable "Prefix" with the Value "SCHEDULER_PARAM_".

...

The command line, which is used to start the script, has no parameters for the script:

Code Block

 powershell ... -file "%SCHEDULER_PARAM_SCRIPT_FILENAME%"

The scripts gets the values for the parameters by reading the environment variable for each parameter (see Passing Parameters to shell-jobs):

Code Block

 $Dir = $env:SCHEDULER_PARAM_FolderName
 $Ext = $env:SCHEDULER_PARAM_FileNameExtension
 .
 ...

...

The script below shows how it works:

Code Block

 # .SYNOPSIS
 #     Creates global PowerShell variables from environment variables
 # .PARAMETER Prefix
 #     Only environment variables with this prefix are converted. The prefix is cut off from
 #     the variable name
 # .Example
 #     .\CreateVariablesFromEnvironment
 #
 #     All environment variables will be converted, e.g. VAR_TEST will become $VAR_TEST
 # .Example
 #     .\CreateVariablesFromEnvironment -Prefix "SCHEDULER_PARAM_"
 #
 #     All environment variables beginning with VAR_ will be converted, e.g. VAR_TEST will become $TEST
 param ([string] $Prefix) 
 # $Prefix = "SCHEDULER_PARAM_"
 
 $envVariables = @(dir env:$Prefix*)
 foreach($envVar in $envVariables)
 \{
     $name = $envVar.Name    
     $name = $name.Substring($Prefix.Length)    
     Set-Variable -name $name -value $envVar.Value -Scope Global
 \}

Executing this script standalone or as part of another script it will parse all environment variables and create for each variable, which name is starting with "SCHEDULER_PARAM_" a powerscript variable. For example, the environment variable "SCHEDULER_PARAM_FOLDERNAME" is then accessible by the script as $FOLDERNAME. This approach is more flexible than the first one, but contrasted to the first one, it is not so easy to find out, which variables from JS are used.

...