ExecutionEngine Documentation

PowerShell & Shell Scripting

  • Last Modified: 2019-07-17

PowerShell is the standard for automating configuration on Windows servers, and Bash/Sh is the standard for automating configuration on Linux servers. OtterScript was designed to seamlessly integrate with both, whether that means running your existing scripts across dozens of servers, leveraging scripts built by the community, or a combination of both.

While you definitely don't need to be a scripting expert, learning how to incorporate scripts into your OtterScript plans will enable you to automate virtually any aspect of Windows or Linux.

Inline Script Execution

Through PSExec, SHExec and Swim Strings, you can incorporate scripts directly into your OtterScript plans.

  • PowerShell
  • Shell
psexec >>
  # delete all but the latest 3 logs in the log directory
  Get-ChildItem "E:\Site\Logs\$ApplicationName" |
     Sort-Object $.CreatedDate -descending |
     Select-Object -skip 3 |
shexec >>
  echo "Hello from $ApplicationName";

The operation will seamlessly replace variables within your PowerShell script, so in the example above $ApplicationName might be defined as a configuration variable.

Calling Scripts

After writing or uploading a script as an asset, you can invoke it with PSCall or SHCall as it was an Operation.

  • PowerShell
  • Shell
pscall CreateUserInDirectory(
  domain: hdars.local,
  user: $UserName

If you use PowerShell's header documentation standards, you'll get descriptions for script arguments when editing in visual mode.

shcall ConfigDirectory(
  arguments: -domain=hdars.local -user=$UserName

Evaluating PowerShell Literals

OtterScript is not a general-purpose programming language, and doesn't have built-in support for arithmetic expressions. You could write a custom variable function to support this, but PSEval is much easier:

For example, if you wanted to convert the value stored in one variable ($minutes) to milliseconds. You could use PSEval to calculate the simple expression $minutes * 60 * 1000:

set $milliseconds = $PSEval($minutes * 60 * 1000);

$PSEval runs the expression on the server currently in context, so use it inside of an If/Else Block to perform different operations depending on the results of the expression on that server.

PowerShell DSC

PowerShell Desired State Configuration (DSC) offers quite a few open-source modules that you can use to configure different features of Windows Servers. With PSDsc, you can invoke DSC Resources in nearly the same manner as a native DSC statement.

PSDsc xWebAppPool (
  Name: AccountsAppPool,
  Ensure: present

These declarative-style statements are essentially Ensure Operations, and thus will collect, report, and remediate drift just like the built-in operations.

Ensuring with Scripts

You can also use PowerShell or Shell scripts to gather and ensure a configuration about a server using PSEnsure and SHEnsure.

Built-in Operations vs Scripts

Because a script can do anything, there's some possible overlap between built-in operations and scripted commands.

In general, you should use a built-in operation when possible. Operations are designed to be massively parallel, cancellation-aware, and highly performant, while also providing rich descriptions when viewed outside of code.

Many scripts, on the other hand, might be written to run interactively, and may prompt for user input if an argument is missing, or do other things not suitable in for non-interactive, scalable environments.

Is this documentation incorrect or incomplete? Help us by contributing!

This documentation is licensed under CC-BY-SA-4.0 and stored in GitHub.

Generated from commit ce197caa on master