Zoomed Image

Writing a Script

Endpoint Management Guide

Script Library

Writing a Script

A script in the Script Library is one block of PowerShell, cmd or VBScript code. Write it with New Script and change it with Edit Script. Both are on Endpoints > Endpoints Dashboard > Setup > Scripts, in the column of buttons above the Script Library list.

New Script

Click New Script. The Script dialog opens.

The Script dialog with the Name, What It Does, Language and Script fields

Field What it does Default
Name The name shown in the library and in Run a Script Blank (required)
What It Does A description for approvers Blank
Language powershell, cmd or vbscript powershell
Script The code. Values come in as environment variables XA_Name. Blank (required)

Click OK. The message reads "Script saved. Your approvers approve it when it is ready." The script appears in the list with the status Draft.

The save is refused when:

  • the name or the code is empty
  • the language is not powershell, cmd or vbscript
  • a line of the code reads END CODE

Values in a script

A value reaches a script as an environment variable. It is never pasted into the code. The approved text stays the same from run to run, so its approval still applies.

A value called Name is read as XA_Name:

Language How to read it
powershell $env:XA_Name
cmd %XA_Name%
vbscript CreateObject("WScript.Shell").Environment("Process")("XA_Name")

Product actions such as Ask the Fleet hand their values over this way.

Edit Script

Click Edit Script. Choose the script in the Script list. Each entry shows its name and status, for example "Restart Print Spooler (Approved)". Click OK and the Script dialog opens with its current text.

A saved change to the code clears its approvals. The dialog says so: "Saving a change clears its approvals: the approval is over the exact text." The script returns to Draft. The person who saved the change becomes its author.

Saving with the code unchanged keeps the status and the approvals. Use this to correct the name or the description.

Approval

A saved script needs approvals from your own approvers only. The number is the DMPCApproveLibraryCount setting (2 by default). Nothing is sent to xAssets. The first approval moves the script from Draft to Pending. See Approving a Script.

Example

A PowerShell script that restarts the print spooler and reports the result:

Restart-Service -Name Spooler -Force
$s = Get-Service -Name Spooler
Write-Output ("Spooler is " + $s.Status)

The output appears against each computer in Activity > Jobs. See Jobs.

Key points

  • Keep each script to one task. A small script is quicker to read and to approve.
  • Write the script so it is safe to run twice. A run cannot be undone by xAssets.
  • A run stops after 300 seconds.