Category filter

How to run scripts on Mac?

A script is a programming language used to automate the execution of certain routine or time-consuming tasks. It is a single file that contains a set of commands or instructions to streamline various processes for Mac management. With scripts, the admin can automate processes to perform specific operations, which otherwise has to be executed one-by-one on Apple’s Terminal.

Hexnode UEM allows IT admins to execute custom scripts on Mac devices remotely. The admin can run any scripts to perform system-level configurations on Mac devices without any user interaction. You can shut down/restart devices, install/uninstall apps, push updates, set up app configurations, and so forth. You can enhance Mac management to the next level by configuring extra settings that may not be natively available in MDM’s features stack.

Note:

  • The Execute Custom Script action is available on Ultimate and Ultra subscription plans.
  • The scripts executed via the Execute Custom Script action run at the root level.

Create and run a Mac script

Here, we will show you how to write a sample bash script that allows you to restart your device.

  1. Open Text Edit from Applications.
  2. Click New Document and write your script in the text area as below:
  3. Now, click Format > Make Plain Text to convert the file into a plain text.
  4. Save the file by clicking on File > Save. Make sure to uncheck the option If no extension is provided, use ‘.txt’ while saving the file. Also, note the file name and location of the file.
  5. Next, navigate to the folder where you have saved the file. Right-click on the file and click Get Info. Unlock the bottom padlock present on the opened info window.
  6. Open Terminal and type the following:

    For example, if you have saved the file in Documents, type as follows:

  7. Next, convert the file into an executable file. Type in

    If the file name is SampleScript, type as follows:

  8. Next, type in ./(filename) to restart your device. In this case, type as follows:

Your device will restart once the script is executed.

Run scripts on Mac using Hexnode UEM

Pre-requisites:

  • Works on devices running macOS 10.11 and later.
  • Supported file formats include Perl (.pl), Bash (.sh), Shell (.sh), C Shell (.csh), Zsh (.zsh), Korn Shell (.ksh), Hypertext Preprocessor (.php), Ruby (.rb), and Python (.py).
  • The latest version of the Hexnode UEM app should be installed on the device.
  • The binary required to run the script must be installed on the Mac.

Warning:


Hexnode recommends you to validate the script manually on a Mac before executing it in bulk.


To execute a custom script on Mac,
  1. From your Hexnode portal, navigate to Manage > Devices.
  2. Click on the Mac device you need to deploy scripts.
  3. Click Actions > Execute Custom Script.
  4. Specify the below parameters to execute the script on that device:
    Choose script file source You can choose the script file either by uploading it or selecting it from the Hexnode repository if the file is already added to Content > My Files.
    Notes:

    • While uploading the script file, make sure the file name does not contain / : ? \ * | “ [ ] @ ! % ^ #.
    • Any new script files uploaded here will also appear on Content > My Files.

    File name The file name will be auto-populated based on the uploaded script or the script chosen from the Hexnode repository.
    Binary path The binary path gets auto-filled based on the type of added script. The binary paths for various types of scripts are listed below:

    Perl: /usr/bin/perl

    Bash: /bin/bash

    Shell: /bin/sh

    C Shell: /bin/csh

    Zsh: /bin/zsh

    Korn Shell: /bin/ksh

    Python: /usr/bin/python

    Hypertext Preprocessor: /usr/bin/php

    Ruby: /usr/bin/ruby

    Note:

    • If /bin/sh is the binary path shown for Bash scripts, update the binary path as “/bin/bash”.

    Arguments Specify the arguments, if needed, while executing the script.
    • You may use any number of arguments for executing the scripts, each of which must be separated by a space. For example, specifying the following string Alexander Sam Richard in the argument field takes in three arguments Alexander, Sam, and Richard, respectively.
    • Hexnode allows the usage of single quotes while providing the arguments. It helps you include multiple words for a single argument. For instance, the following string, ‘William Alexander’ ‘Sam Anderson’, specifies two arguments containing multiple words.
      Note:


      Note: Avoid inclusion of space in the beginning or end of the single-quoted multi-word argument.

      ‘ William Alexander ’ ‘ Sam Anderson ’ ❌

      ‘William Alexander’ ‘Sam Anderson’ ✅

    • For displaying a normal single quote within an argument, you should add two single quotes instead of one. Quoting an example, specifying the string William’’s Arcade takes in a single argument of the form of William’s Arcade while executing the script.
    • Consider the case of a shell script (say, file_search.sh) used to search a file (Samplefile.txt) in the Desktop folder. The script is shown below:
      Script to find a file

      This script requires two arguments which are indicated by $1 and $2 respectively. To run this script, we provide the arguments as:
      /Users/yourname/Desktop Samplefile.txt

    • It also supports the use of the following wildcards:
      • %devicename%
      • %model%
      • %serialnumber%
      • %deviceid%
      • %udid%
      • %phonenumber%
      • %wifimacaddress%
      • %name%
      • %email%
      • %username%
      • %domain%
      • %netbiosname%
      • %ssid%
      • %assettag%
      • %department%
      • %devicenotes%
      • %alternateemail%
      • %newline%
      • %null%

      For example, specifying the string %name% %email% in the arguments field retrieves the name and email address of the user as the two arguments.

  5. Click the Execute button to confirm your action.
  6. Now, navigate to the Action History sub-tab to view the execution status of the script. You can see the output details by clicking on the Show Output button corresponding to the execution status of that script.
  7. The script, if coded correctly, will be executed successfully to automate the specified operations on the device.

    Execute or run custom scripts on mac using mdm

  • Remote Actions