...
using command line parameters
using position parameters
Code Block |
---|
powershell ... -file "%SCHEDULER_PARAM_SCRIPT_FILENAME%" "%SCHEDULER_PARAM_FolderName%" "%SCHEDULER_PARAM_FileNameExtension%" |
...
The better approach is to use the parameters by name.
using named parameters
Starting a script like below:
Code Block |
---|
.\CreateVariablesFromEnvironment -Prefix "VAR_"
|
and the first statement in the script is the param-statement:
Code Block |
---|
param ([string] $Prefix)
|
will fill the variable "Prefix" with the Value "VAR_".
using environment variables
JobScheduler can provide order- and job-parameter as environment variables to the shell.
using explicit variables
The command line, which is used to start the script, has no parameters for the script:
...
Code Block |
---|
$Dir = $env:SCHEDULER_PARAM_FolderName
$Ext = $env:SCHEDULER_PARAM_FileNameExtension
.
...
|
Defining all needed variables at the beginning of the script will give one an overview, which variables are used from JobScheduler. In other words, this could be part of an integrated automatic documentation.
using by parsing all environment variables
JobScheduler provide more environment variables as the one which are related to a parameter. it is possible to get all this variables as "internal" named variables into the powerscript.
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_FOLDER_NAME" is then acessible with the script as $FOLDER_NAME. 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.
The advantage to use parameters by this method environment variables is that the access to the parameters is done by the Name of the parameter, not by the position of the name and/or value within the command line. Better: no parameter values on the command line are required.
And this solution will allow the possibility to create an universal (generic) job to run powershell scripts.
related downloads:
see also:
- How to execute a PowerShell script with JS
- Setting the executionPolicy to execute .ps1-scripts
- How to get the Exit code of a powershell script
- A generic PowerShell Job
...