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 MDM 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.

Create and Run a Mac Script

Here, we will show you how to write a sample bash script that prints “Hello, world!” to the screen.

  1. Open Text Edit from Applications.
  2. Click New Document and write your script in the text area as below:
    #!/bin/sh 
    
    echo "Hello, world!" 
  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:
    cd (location of the script) 

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

    cd Documents 
  7. Next, convert the file into an executable file. Type in
    chmod 700 (filename) 

    If the file name is SampleScript, type as follows:

    chmod 700 SampleScript
  8. Next, type in ./(filename) to print “Hello, world!” to the screen. In this case, type as follows:
    ./SampleScript

You can see “Hello, world!” printed on the screen.

Run Scripts on Mac using Hexnode MDM

Pre-requisites:

  • Works on macOS 11.11+.
  • 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).
  • It requires Hexnode MDM app v1.2+ installed on the device.
  • The binary required to run the script must be installed on the Mac.
  • This feature is available only on Ultimate and Ultra subscription plans.

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. 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%
    • %deviceid%
    • %wifimacaddress%
    • %name%
    • %domain%
    • %email%
    • %imei%
    • %username%
    • %department%
    • %assettag%
    • %devicenotes%
  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.