Lesson 25: Scripting and Jamf Pro

Jamf 100 Course

Solution
Application
Content Type
Training Content
Utilities & Services
ft:locale
en-US

Goal

Learn what a script is and how it can be used. Upload a basic script to Jamf Pro and deploy it to a Mac.

Video

Key points

  • Scripts can be used to automate tasks and extend the functionality of Jamf Pro.

  • A script is a collection of commands in a single file designed to be run in a specific order.

  • The open command opens files, directories, or URLs. For example:

    • open https://jamf.com will open the Jamf website in the default browser.

    • open -a Chess.app will open the Chess application.

      • If no application or browser is specified using the -a flag, the system default will be used.
  • The sleep command suspends command execution for a period of time.

    • For example, sleep 5 will pause script execution for five seconds.

  • Jamf binary commands can be called in scripts in the same manner as they are used in Terminal.

    • For example, jamf displayMessage -message will use the jamf binary to display a message on the computer.

  • Scripts are run on macOS using a command-line interpreter, or shell, that is used to read and execute the commands contained within a script.

  • Every script needs to specify a command-line interpreter in the first line. The script must start with a shebang (#!) immediately followed by the path to a specific shell.

    • For example, #!/bin/bash tells macOS to use the Bash shell to run this script.

  • Scripts deployed from Jamf Pro are automatically run as the root user.

  • Comments can be added to a script using a pound sign (#).

    • It is good practice to add comments throughout your script that explain what subsequent code does.

Review

To view answers, click arrow next to each question.

  1. This line, comprised of a shebang and file path, tells macOS what command-line interpreter or shell should be used for the script. This is always the first line of a shell script, and in this case, specifies the Bash shell.
  2. Comments are added using a pound sign (#) and are used to explain the function of a script to anyone who may edit or use it in the future.
  3. The sleep command pauses code execution for a specified period of time; for example, sleep 5 will cause the shell to wait for five seconds before proceeding to the next line of the script.

Practice

  1. Create and save a new script.

    1. Navigate to Settings > Computer management > Scripts.

    2. Click New.

    3. Enter a display name of your choosing.

    4. On the Script tab, paste the following:

      #!/bin/sh
      
      #Open the Chess app
      open -a Chess.app
      
      #Wait three seconds
      sleep 3
      
      #Display a message to the user
      jamf displayMessage -message 'Let's play Chess!'
    5. Click Save.

  2. Create a new policy to make the script available in Self Service.

    1. Navigate to Computers > Policies.

    2. Click New.

    3. Enter a display name of your choosing.

    4. In the Execution Frequency pop-up menu, select "Ongoing".

    5. Select the Scripts payload and add the script created in Task 1.

    6. Scope the policy to a test computer.

    7. On the Self Service tab, select the Make the policy available in Self Service checkbox.

    8. Save the policy to make it available in Self Service.

    9. Open Self Service on the test computer and run the policy.

Resources

Jamf Pro Documentation

Jamf

Terminal User Guide

Apple

W3Schools.io