Skip to content

Instantly share code, notes, and snippets.

@apizz
Last active July 14, 2020 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apizz/c361bb1b8ee84829e3d02b4e3dffa98d to your computer and use it in GitHub Desktop.
Save apizz/c361bb1b8ee84829e3d02b4e3dffa98d to your computer and use it in GitHub Desktop.
Script to be run after rename-comp.py to verify the configured hostname before continuing to provision a Mac.
#!/bin/bash
# Jamf script to be run after rename-comp.py to verify the configured hostname before continuing to provision
# a Mac.
#
# Usage:
#
# 1) Add this script to your Jamf instance
# 2) Configure the CSV variable below to match what you have listed in your rename-comp.py script
# 3) Add this script to your policy so that it runs AFTER rename-comp.py
# 4) Configure Parameter 4 for this script to match the URL configured in Parameter 4 of rename-comp.py
# 5) Configure Parameter 5 for this script to match the custom trigger configured for your policy to manually
# set the Mac's hostname
# 6) Configure Parameter 6 for this script to match the custom trigger configured for your subsequent Jamf bind
# and/or other enrollment policies
# Define path to locally cached CSV of serial #s and hostnames. This must match what's listed in
# rename-comp.py (https://gist.github.com/haircut/1debf91078ce75612bf2f0c3b3d99f03#file-rename-computer-py)
# Jamf script.
CSV='/var/tmp/computernames.csv'
# Get hostname
COMPUTERNAME=$(/bin/hostname)
# Get serial #
SERIAL=$(/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number/{print $4}')
# Script parameter 4 URL must match the parameter 4 value of the rename-comp.py
# (https://gist.github.com/haircut/1debf91078ce75612bf2f0c3b3d99f03#file-rename-computer-py) script in the
# same policy.
# Set URL to Parameter 4
URL="$4"
# Set Parameter 5 variable
MANUAL_RENAME_POLICY_TRIGGER="$5"
# Set Paramater 6 variable
ENROLLMENT_CONTINUE_TRIGGER="$6"
# Verify CSV exists, if not download it if able
if [ ! -f "$CSV" ]; then
/bin/echo "Cached CSV not found. Downloading ..."
/usr/bin/curl "$URL" -o "$CSV"
fi
# Get hostname from cached CSV with serial #. Use 'head' to get first one, just in case of duplicates ...
CSV_COMPUTERNAME=$(/bin/cat "$CSV" | /usr/bin/tr ',' ' ' | /usr/bin/grep "$SERIAL" | /usr/bin/head -1 | /usr/bin/awk '{print $2}')
# So long as current computer name matches what's in the CSV, continue with enrollment.
# If not, produce error, as this is likely the result of the machine not being listed in the CSV
# and switch to manual naming.
if [ "$COMPUTERNAME" = "$CSV_COMPUTERNAME" ]; then
/bin/echo "Successfully set hostname to ${COMPUTERNAME}!"
jamf policy -event "$ENROLLMENT_CONTINUE_TRIGGER"
else
/bin/echo "ERROR: Current hostname does not match or does not exist in the CSV. Please check the CSV."
/bin/echo "Switching to manual computer naming ..."
# Run standalone rename policy
jamf policy -event "$MANUAL_RENAME_POLICY_TRIGGER"
# If successful, trigger enrollment continued
if [ "$(/bin/echo $?)" = 0 ]; then
jamf policy -event "$ENROLLMENT_CONTINUE_TRIGGER"
else
/bin/echo "Rename Failed. Exiting ..."
exit 1
fi
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment