There are at least two different approaches to pass parameters to a ps-script:
- as command line parameters
- as environment variables
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:
<order title="Executes the script ListFilesByExtension.ps1"> <params> <param name="Script_Filename" value="ListFilesByExtension.ps1"/> <param name="FolderName" value="c:\temp"/> <param name="FileNameExtension" value=".txt"/> </params> <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:
$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 command line parameters
powershell ... -file "%SCHEDULER_PARAM_SCRIPT_FILENAME%" "%SCHEDULER_PARAM_FolderName%" "%SCHEDULER_PARAM_FileNameExtension%"
$Dir = $args[0] $Ext = $args[1] . ...
Depending on the parameters every job has to be different. No general solution for a generic powershell job is possible. That will more or less increase the effort for maintenance of jobs.
In Addition, acessing at parameter by its position on the command line can lead into errors when anyone changes the commandline without change the script or the same script will be used by different jobs - with at the end - different command lines.
The better approach is to use the parameters bei its name.
using environment variables
The command line, which is used to start the script, has no parameters for the script:
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):
$Dir = $env:SCHEDULER_PARAM_FolderName $Ext = $env:SCHEDULER_PARAM_FileNameExtension . ...
The advantage to use parameters by this method is that the access to the parameters is done by the Name of the parameter, not by the position of the value within the command line. Better: no parameter values on the command line are required.
see also: