Skip to main content

1E SDK

Scripting.RunText

Method

Run

Module

Scripting

Library

Scripting

Action

Execute the specified script supplied as text.

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.

Text (string): The text of the script.

Warning

The text should be in an appropriate format for the target operating system. For example, a multi-line bash script should have lines separated by just linefeed (newline) characters, not Windows' carriage-return + linefeed.

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 function to convert to JSON; an example is provided here _Using PowerShell commands and scripts.

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. However, "Bypass" is probably the most useful and "Undefined" is pointless.

In addition to the standard execution policy values, there is an additional special " Override " value. This allows the host's execution policy to be bypassed completely, even at theMachinePolicyandUserPolicyscopes. In other words, "Override" allows a PowerShell script to run whatever the local settings

ExecutionPolicy is 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 1E Client 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.

User (string): Aligns the row output to the user that the output refers to.

Example

This generates an Output value of "Hello\nGoodbye" where \n is a newline character.

Scripting.RunText(Language: "Bash", Text: "echo Hello; echo Goodbye");

AsUser example

@users = Users.GetLoggedOnUsers();
@allExecutions = FOREACH @user IN @users
DO
    @account = SELECT Account FROM @user;
    Scripting.RunText(Language: "Powershell", Text:"echo Hello; echo Goodbye", AsUser:@account);
DONE;

Passing a path as the Text

It is possible to use this method to execute a pre-existing script file as the Text parameter exactly as if within a PowerShell prompt, for example:

Scripting.RunText(Language:"PowerShell",
Text:"C:\\ProgramData\\1E\\simpleEcho.ps1 $($args[0]) $($args[1])","hello","world"); 

Script arguments must be explicitly forwarded as shown, as only the author of the above line knows exactly where the parameters should go within the text field.

However, the above is really what the Scripting.Run() method is designed for, which makes things far more readable.

Platforms

  • Windows - PowerShell

  • Linux - bash

  • MacOS - bash

Notes

This is an alternative to the Scripting.Run method which instead uses a script in a file.