Skip to main content

1E 9.x (on-premises)

Scripting.Run

Module

Scripting

Method

Run

Library

Scripting

Action

Execute the specified script file.

Parameters

Language (string): The shell language of the script. Currently "PowerShell" and "bash" (case is not significant) are supported.

LanguageVersion (string; optional): If specified, the version of the script interpreter, e.g. "3.0" for PowerShell if features not supported in older versions are used. A runtime error occurs if the requested version of the interpreter is not available on the host.

ScriptPath (string): The full path of the script file.

Note

While the earlier "Script" name will still work, we recommend that ScriptPath is used instead.

Warning

The script file must be in a format suitable for the target O/S. For example, the line-end character for a bash script must be just newline, not Windows-style carriage-return + newline.

Tip

Recommended to be local. Use the httpGetFile() function to download a file. The output of the function is FilePath.

InterpretAsJson (boolean; optional, default false): If specified and has value true, assume that the output of the script is JSON and deserialize it. The return value is then the table produced.

Warning

If InterpretAsJson is set to true, then the output of the script must be serialized as JSON. PowerShell 3.0 and later has a ConvertTo-Json cmdlet. If using PowerShell 2.0 then LanguageVersion must be set to 2.0 and your script will need to include a custom function to convert to JSON.

TimeoutSecs (integer; optional, default 3600): If the script does not terminate within the specified number of seconds, return anyway. A value of 0 means an eternal timeout, i.e. none. A negative value is illegal.

TimeoutKill (boolean; optional, default false): If a timeout has been specified and is reached, attempt to kill the script execution sub-process. The method terminates even if killing the script fails for some reason, i.e. the method never hangs. Do not specify if there is no timeout.

Warning

Try to avoid using TimeoutKill if possible, and instead let the sub-process die in its own time. For example, consider the effect of killing an install partway through. Also, only the script interpreter is killed, not any sub-sub-processes it may invoke.

ExecutionPolicy (string; optional, default empty): Can only be used with PowerShell scripts. If specified, set the named PowerShell execution policy at the Process scope (i.e. as if the command "powershell.exe -ExecutionPolicy policy ..." was used to execute the script). This can be used to get round lower-scope execution policy settings that might otherwise block script execution. All PowerShell execution policy values are accepted, including " Bypass". However ExecutionPolicy provides an additional special " Override " value, which allows the host's execution policy to be bypassed completely, even at the MachinePolicy and UserPolicy scopes. In other words, "Override" allows a PowerShell script to run whatever the local settings.

ExecutionPolicy values are not case-sensitive.

IgnoreExitCode (boolean, optional, default false): If true, the execution of the script will be considered successful, irrespective of the exit code returned. If false, any non-zero exit code will be interpreted as a failure and this method will return an error.

Note

IgnoreExitCode only applies if the script actually executes and terminates. This is not a general "keep going regardless of anything failing" switch. For example, if an attempt is made to run a PowerShell script but powershell.exe itself is broken, then execution of the method fails regardless of the IgnoreExitCode setting.

WorkingDirectory (string, optional): This will set the working directory for the script execution to the specified path. If the path does not exist, the agent will return an error.

script_params ... (optional): Zero or more "anonymous" parameters (i.e. just values, with no " name:" prefix) that will be passed directly to the script as its command line parameters. They can be of various types (string, int, etc.). The script parameters can be included anywhere in the list of method parameters, because they are anonymous.

Return values

If parameter InterpretAsJson was true, the data table produced.

Otherwise:

ExitCode (int): The exit code from execution of the script. -2 if the script timed out.

Warning

Depending on the script, an exit code of 0 does not necessarily mean that execution was successful.

Output (string): The text produced by the script's execution on standard output and standard error streams.

Example

The first PowerShell example runs a script that itself takes 2 positional parameters with values "script param 1" and 42; abandon (but do not kill) the script if it does not end within 5 seconds. Run the script regardless of PowerShell execution policy.

This also shows that usually a call to the Scripting.Run method is used in combination with the httpGetFile() function so that the agent itself supplies the local copy of the script file.

@resource1 = HttpGetFile(URL:"myscript.ps1", Size:483, 
Hash:"ff3f4bd8fb4e0e0ae0caeef8eb03cbc6dbf4d61b005bb5f43427710286811803");
Scripting.Run(Language: "PowerShell", LanguageVersion: "3.0", ScriptPath: @resource1, 
TimeoutSecs: 5, ExecutionPolicy: "Override", "script param 1", 42);

This next PowerShell example runs the script as each of the logged-on users (if any):

AsUser example

@script = HttpGetFile(URL:"myscript.ps1", Size:483, 
Hash:"ff3f4bd8fb4e0e0ae0caeef8eb03cbc6dbf4d61b005bb5f43427710286811803");

@users = Users.GetLoggedOnUsers();
@allExecutions = FOREACH @user IN @users
DO
    @account = SELECT Account FROM @user;
    Scripting.Run(Language: "PowerShell", LanguageVersion:"3.0", ScriptPath:@script, 
    TimeoutSecs:5, AsUser:@account, "script param 1", 42);
DONE;

This Bash example is rather artificial but shows the creation of a temporary bash script then execution of the script to produce "Hello" as the Output value (and an ExitCode of 0).

NativeServices.RunCommand(CommandLine: "/bin/bash -c 'echo echo Hello > /tmp/temp'");
Scripting.Run(Language:"Bash", ScriptPath:"/tmp/temp");

Platforms

  • Windows - PowerShell

  • Linux - bash

  • MacOS - bash

Notes

This is an alternative to the Scripting.RunText method which instead uses script text supplied as a parameter.