Contents

Method

Run

ModuleScripting
LibraryScripting
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.

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

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.

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.

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.

Timeout is not supported on Android. It is not an error to specify the parameter but the value will be ignored.

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.

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.

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. This is not supported on Android.

AsUser (string; optional; default 1E.Client service user) Windows only: The "domain\user" (down-level logon) account name that the Agent will attempt to impersonate on the local machine.

If this parameter is absent the command will be executed in the Tachyon Agent process's user context.

It is not an error if the specified user is not currently logged on (and this includes accounts that are not even defined). In this case execution is deemed successful but the method does nothing and returns no data.

The specified user must be currently logged on and running explorer.exe (Windows Explorer).

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.

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
  • Solaris Intel - bash
  • Solaris Sparc - bash
  • Android - bash (limited support - see notes below)
Notes

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

Because the Tachyon Agent has multiple workers and so can run a number of instructions in parallel, it is possible that conflicting scripts could run at the same time, e.g. each attempting to install software. (Software packages can only usually be installed one at a time.) If this is likely to be a problem, the Agent's "Exclusivity" configuration value should be set to true which will ensure that only one script can run at any time across all workers, causing other scripts to be temporarily suspended. By default Exclusivity is false. Exclusivity was introduced in later versions of v4.1; prior to then scripts would block, equivalent to Exclusivity=true.

This method does not have a SplitLines parameter. If the script output is a single text block and not JSON, then you can use the Utilities.SplitLines method to split the text block into lines.

PowerShell script parameters are in effect surrounded by single quotes when the script is executed. This is to allow parameter values to contain embedded spaces, and to allow what can be metacharacters (e.g. '(' for PowerShell or '$' for bash) to be treated as literals. This means that PowerShell script parameters cannot contain embedded single quotes. bash scripts can contains embedded quotes. Android scripts also support embedded quotes.

If the Agent is shut down while a script is running (with or without a timeout specified), the script subprocess is detached from the Agent (so that Agent shutdown is not blocked), and the Tachyon instruction's status is set to "execution failed" because Tachyon cannot know the ultimate fate of the script. It is up to the user to decide whether to resubmit the instruction or not when the Tachyon Agent resumes.

By default, a long-running instruction will time out. "Long running" is 3600 seconds, i.e. 1 hour. This is to prevent a hanging script from completely blocking the Tachyon Agent, because the Agent has an exclusivity feature for safety such that even if the Agent runs with multiple workers then executing a script prevents any other worker from running an instruction in parallel. (This is because instructions that follow the script's execution might depend on that script having run to completion.) A script hanging indefinitely would completely block the Agent, including any instructions to kill the hanging script's process. If a script really must be run to completion, set TimeoutSecs to 0 to indicate no timeout.

For Android, although the method is supported, the Android sandbox environment is usually so restrictive that scripts cannot successfully run many commands, especially commands that access the filesystem.