Otter Documentation

PowerShell & Shell Scripting

PowerShell is the standard for automating configuration on Windows servers, Bash/Sh is the standard for automating configuration on Linux servers. Otter 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 custom combination of both.

While you definitely don't need to be a scripting guru to use Otter, learning how to incorporate scripts into your Otter 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 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 |
     Remove-Item
>>;
shexec >>
  echo "Hello from $ApplicationName";
>>;

Otter 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 scripts as an Asset, you can invoke them with PSCall or SHCall as if they were an Operation.

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

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

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

Evaluating PowerShell Literals

The Otter Execution Engine is not a general-purpose programming runtime, and thus doesn't have built-in support for things like arithmetic. You could, of course, 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 PSEval the simple expression $minutes * 60 * 1000:

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

$PSEval actually 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 configuration about a server using PSEnsure and SHEnsure.

Built-in Operations vs Scripts

Because a script can do anything, there's an obvious overlap between Otter's built-in operations and a PowerShell CmdLet or script that you might write.

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

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

More on this topic: