> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/sonar/administration/automation/scripts.md). Agents can also search and read this documentation through its [MCP server](https://wiki.datadike.com/~gitbook/mcp).

# Script library

Build and maintain the script library under Management › Scripts: create, upload, clone, test, favorite, hide and download scripts, and reuse code with snippets.

## What this is and when to use it

The script library is the single place where SONAR keeps the scripts it runs on devices. Every script run from the console comes from here: **Run Script** on one device, **Tools › Bulk Script** on many, script actions in automated tasks, script checks, and the actions of alert templates. A script carries its own defaults (shell, arguments, environment variables, timeout, supported platforms), so whoever runs it later only changes what is different.

Use the library to write a script once, test it against one device before it reaches a fleet, and keep reusable blocks of code as snippets.

## Before you start

* A role with **List Scripts** to open the library, and **Manage Scripts** to create, upload, clone, edit or delete scripts and snippets. Both are in the **Scripts** group of **Admin Center › Roles**.
* To test a script, **Run Scripts** (group **System**) and access to the device you test on. The device must be online.
* For Linux and macOS, a script of type **Shell** or **Python** needs a shebang on its first line, for example `#!/bin/bash` or `#!/usr/bin/python3`.

## Steps

### Create a script

1. Open **Management › Scripts**.
2. Click the **+** button and choose **New Script**.
3. Fill **Name**, **Shell Type** and, if the script is not for every platform, **Supported Platforms**. See the field reference below.
4. Type or paste the code in the editor on the right.
5. Optionally set **Category**, **Script Arguments**, **Environment vars**, **Timeout (seconds)**, **Run As User (Windows only)** and **Syntax**.
6. Click **Save**.

### Upload a script from a file

1. Open **Management › Scripts**, click **+** and choose **Upload Script**.
2. In **Add Script**, fill **Name** and choose the file in **Script Upload**.
3. Set **Type**, **Supported Platforms**, **Category**, **Script Arguments**, **Environment Variables** and **Default Timeout** as needed.
4. Click **Add**. The file content becomes the script body.

### Test a script against one device

1. Open the script with a double-click, or right-click it and choose **Edit**.
2. In **Agent to run test script on**, at the bottom of the dialog, pick the device.
3. Click **Test Script**. The **Script Test** dialog runs the code currently in the editor, with the arguments, environment variables, timeout and **Run As User** setting of the form, and waits for the result.
4. Read **Run Time**, **Return Code**, **Standard Output** and **Standard Error**.
5. Save the script only when the result is what you expect.

The test uses what is in the editor, saved or not, so you can iterate without saving each attempt.

### Clone, favorite, hide, download or delete

Right-click a script in the list to open its menu:

| Menu entry                                   | What it does                                                                                                                                  |
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| **View Code**                                | Opens the script read-only, with snippets already expanded                                                                                    |
| **Clone**                                    | Opens a copy named `(Copy) <name>` for editing. The way to adapt a built-in script                                                            |
| **Edit**                                     | Opens the script for editing. Not available for built-in scripts                                                                              |
| **Delete**                                   | Deletes the script after confirmation. Not available for built-in scripts                                                                     |
| **Add as Favorite** / **Remove as Favorite** | Marks the script as a favorite. Favorites are listed under **Run Favorited Script** in every device's action menu                             |
| **Download Script**                          | Saves the script as a file named after it, with the extension of its shell (`.ps1`, `.bat`, `.py`, `.sh`, `.nu`, `.ts`) and snippets expanded |
| **Hide Script** / **Show Script**            | Hides the script from the library and from the script pickers used to run it                                                                  |

Favorites and hidden scripts are properties of the script, not of the user: every console user sees the same favorites.

{% hint style="warning" %}
Deleting a script also deletes the script checks that use it, and removes it from the actions of automated tasks. Check where a script is used before you delete it. Hiding it is the reversible alternative.
{% endhint %}

### Reuse code with snippets

A snippet is a named block of code that SONAR inserts into a script body when the script runs.

1. In **Management › Scripts**, open the ⋮ menu and choose **Script Snippets**.
2. Click **New**, fill **Name**, **Shell Type** and **Description**, write the code and click **Save**.
3. In any script body, write the snippet name between double braces, for example `{{Write-Log}}`.

When the script runs, is tested or is downloaded, the placeholder is replaced by the snippet code. **Edit** shows the placeholder; **View Code** shows the expanded result. Double-click a snippet to edit it; right-click it to delete it.

Placeholders for device, client, site, custom field and key store values, such as `{{agent.hostname}}`, work in **Script Arguments** and **Environment vars**, not in the script body. See [Script variables](https://wiki.datadike.com/sonar/reference/script-variables.md).

## What you should see

The library opens in a table with the columns **Favorites**, **Shell**, **Platforms**, **Name**, **Default Args**, **Category**, **Description** and **Default Timeout (seconds)**. A script with no supported platforms shows **All**. Hidden scripts appear in grey when **Show Hidden Scripts** is on.

The ⋮ menu switches between **Table** and **Folders**, and shows or hides the built-in scripts with **Show Community Scripts** / **Hide Community Scripts**. The folder view groups scripts by category, with the scripts that have none under **Unassigned**.

After **Save** or **Add**, a confirmation such as `<name> was added!` or `<name> was edited!` appears and the script is in the list.

## How to verify it worked

1. Find the script in **Management › Scripts** with the search box, and check its **Shell**, **Platforms**, **Default Args** and **Default Timeout (seconds)** columns.
2. Open **Run Script** on a device of a supported platform (see [Run a command or a script on a device](https://wiki.datadike.com/sonar/administration/automation/run-commands-and-scripts.md)). The script appears in the picker and its defaults fill the form.
3. Open **Reports & Audits › Audits**. Creating, editing and deleting a script are recorded as **Add Object**, **Modify Object** and **Delete Object** on a **Script**. A test run is recorded as **Execute Script** with the message `<user> tested a script on <device>`.

A test run does not create a line in the device's **History** tab; runs from **Run Script**, bulk actions and tasks do.

## Common errors

| Symptom                                                                                                                | Cause                                                                                                         | Fix                                                                                                 |
| ---------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| The **+** button and **Edit** are greyed out, with the tooltip `You don't have permission for this action`             | The role lacks **Manage Scripts**                                                                             | Add the permission to the role, or ask someone who has it                                           |
| **Edit** and **Delete** are greyed out, with `Built-in scripts can't be edited` or `Built-in scripts can't be deleted` | The script is built-in                                                                                        | Use **Clone** and edit the copy                                                                     |
| The editor shows `Shell/Python scripts on Linux/Mac need a shebang at the top of the script`                           | A **Shell** or **Python** script has no `#!` line                                                             | Add the interpreter line at the top. Ignore the banner for scripts that only run on Windows         |
| **Test Script** stays disabled                                                                                         | No device chosen, the body is empty, or the timeout is empty                                                  | Pick a device in **Agent to run test script on**, add code and a timeout                            |
| The test returns no output                                                                                             | The device is offline, or the script ran for its whole timeout                                                | Check the device status, or raise **Timeout (seconds)** and test again                              |
| A script does not appear in **Run Script**                                                                             | It is hidden, it does not support the device's platform, or it is built-in while community scripts are hidden | Use **Show Script**, add the platform in **Supported Platforms**, or use **Show Community Scripts** |
| Saving a snippet fails with `script snippet with this name already exists.`                                            | Snippet names are unique                                                                                      | Choose another name                                                                                 |
| **Default Timeout** shows `Minimum is 5` in **Add Script**                                                             | Timeouts shorter than 5 seconds are not accepted                                                              | Enter 5 or more                                                                                     |

## Field reference

### Script (New Script, Edit)

| Field                                        | What it does                                                                                                                                                                                                      | Accepted values                                                         | Default                |
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- | ---------------------- |
| Name                                         | Identifies the script in the library and in every picker                                                                                                                                                          | Text, up to 255 characters                                              | Empty. Required        |
| Description                                  | Shown in the list                                                                                                                                                                                                 | Free text                                                               | Empty                  |
| Shell Type                                   | Interpreter that runs the script on the device                                                                                                                                                                    | **Powershell**, **Batch**, **Python**, **Shell**, **Nushell**, **Deno** | **Powershell**         |
| Supported Platforms (All supported if blank) | Restricts the script to these platforms. Pickers on a device only offer the scripts that support its platform                                                                                                     | **Windows**, **Linux**, **macOS**, several allowed                      | Blank (all platforms)  |
| Category                                     | Groups the script in the folder view. Pick an existing category or type a new one                                                                                                                                 | Text, up to 100 characters                                              | Empty (**Unassigned**) |
| Script Arguments                             | Default arguments, one per entry. Press Enter after each. Can contain [script variables](https://wiki.datadike.com/sonar/reference/script-variables.md)                                                           | Text                                                                    | None                   |
| Environment vars                             | Default environment variables, one per entry, as `KEY=VALUE`. Press Enter after each. The value can contain script variables                                                                                      | Text                                                                    | None                   |
| Timeout (seconds)                            | Default maximum run time. Whoever runs the script can change it                                                                                                                                                   | Whole number, 5 or more                                                 | 90                     |
| Run As User (Windows only)                   | Runs the script in the context of the logged-in user. When set on the script, it always applies and overrides the **Run As User** choice made when running it. If no user is logged in, the script runs as SYSTEM | On or off                                                               | Off                    |
| Syntax                                       | Free text describing the expected arguments. Shown to whoever runs the script                                                                                                                                     | Free text                                                               | Empty                  |
| Editor                                       | The script body. May contain snippet placeholders such as `{{Snippet Name}}`                                                                                                                                      | Code                                                                    | Empty                  |
| Agent to run test script on                  | Device used by **Test Script**                                                                                                                                                                                    | A device you can access                                                 | None                   |

### Upload Script (Add Script)

| Field                                        | What it does                                    | Accepted values | Default         |
| -------------------------------------------- | ----------------------------------------------- | --------------- | --------------- |
| Name                                         | As above                                        | Text            | Empty. Required |
| Description                                  | As above                                        | Free text       | Empty           |
| Category                                     | As above. Press Enter or Tab to add a new value | Text            | Empty           |
| Script Upload                                | The file whose content becomes the script body  | A text file     | None. Required  |
| Type                                         | Same as **Shell Type**                          | As above        | **Powershell**  |
| Supported Platforms (All supported if blank) | As above                                        | As above        | Blank           |
| Script Arguments                             | As above                                        | Text            | None            |
| Environment Variables                        | As above                                        | `KEY=VALUE`     | None            |
| Default Timeout                              | As above                                        | 5 or more       | 90              |

### Snippet

| Field       | What it does                                                        | Accepted values           | Default         |
| ----------- | ------------------------------------------------------------------- | ------------------------- | --------------- |
| Name        | The name used in the placeholder `{{Name}}`. Unique                 | Text, up to 40 characters | Empty. Required |
| Shell Type  | Language of the snippet, for your reference and editor highlighting | Same list as scripts      | **Powershell**  |
| Description | Shown in the snippet list                                           | Text, up to 50 characters | Empty           |
| Editor      | The code inserted in place of the placeholder                       | Code                      | Empty           |

## Related pages

* [Run a command or a script on a device](https://wiki.datadike.com/sonar/administration/automation/run-commands-and-scripts.md)
* [Run actions across many devices](https://wiki.datadike.com/sonar/administration/automation/bulk-actions.md)
* [Automated tasks](https://wiki.datadike.com/sonar/administration/automation/automated-tasks.md)
* [Script variables](https://wiki.datadike.com/sonar/reference/script-variables.md)
* [Roles and permissions](https://wiki.datadike.com/sonar/administration/identities/roles.md)
