Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Created May 23, 2024 05:47
Show Gist options
  • Save scriptingosx/931a8cb4f626c6ff7513fbba39786faf to your computer and use it in GitHub Desktop.
Save scriptingosx/931a8cb4f626c6ff7513fbba39786faf to your computer and use it in GitHub Desktop.
Sample script to set the computer name according to my 2024 MacAD.UK presentation: https://scriptingosx.com/macaduk2024
#!/bin/bash
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
####################################################################################################
# License information
####################################################################################################
#
# Copyright (c) 2024, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# Set Computer Name
#
# Version: 1.2
# Written by: Armin Briegel | Consulting Engineer | Jamf
#
# DESCRIPTION
# This script will assemble a computer name from a prefix and a certain number of digits
# from the end of the serial number or hardware UUID and then set the computer name.
#
# There are different approaches for each part (prefix and identifier) in the code. You
# can uncomment the sections you want to use, and delete or comment the parts you do not
# need.
#
# Note: this script uses `awk` to parse the output of `system_profiler` for backwards
# compatibility with macOS Monterey and higher, you can (and probably should) replace
# the `awk` calls with `plutil`:
#
# model family:
# family=$(system_profiler -xml SPHardwareDataType | plutil -extract '0._items.0.machine_name' raw -)
#
# serial number:
# identifier=$(system_profiler -xml SPHardwareDataType | plutil -extract '0._items.0.serial_number' raw -)
#
# hardware UUID:
# identifier=$(system_profiler -xml SPHardwareDataType | plutil -extract '0._items.0.platform_UUID' raw -)
#
####################################################################################################
# name is constructed from prefix and the last digits of the identifier
# constant prefix
# prefix="Mac"
# get model family from system_profiler
family=$(system_profiler SPHardwareDataType | awk -F': ' '/Model Name/ {print $NF}')
# different prefix for desktop vs laptop
if [[ ${family} == *MacBook* ]]; then
# laptop
prefix="MacBook"
else
# desktop
prefix="Mac"
fi
# use family as prefix (clean out whitespace)
# prefix=$(tr -d '[:space:]' <<< "${family}")
# use hardwareUUID as identifier
# identifier=$(system_profiler SPHardwareDataType | awk '/Hardware UUID/ {print $NF}')
# use serial number as identifier
identifier=$(system_profiler SPHardwareDataType | awk '/Serial Number/ {print $NF}')
# use n digits of the identifier
# depending on your fleet size, 4-6 digits are sensible values
numDigits=5
# get trailing digits (works best with UUID)
# offset=$((${#identifier} - numDigits))
# get center digits (works best with pre-2021 serial numbers)
offset=$(( (${#identifier} - numDigits) / 2 ))
digits=${identifier:${offset}:${numDigits}}
# verify we actually got the right number of digits
if [[ ${#digits} -ne ${numDigits} ]]; then
echo "something went wrong parsing the identifier, $identifier, $digits"
exit 2
fi
# assemble the name
name="${prefix}-${digits}"
# clean out special chars for hostnames
hname=$(tr -d "[:blank:]'&()*%$\"\\\/~?!<>[]{}=+:;,.|^#@" <<< "${name}")
# check if running as root
if [[ $EUID -ne 0 ]]; then
echo "this script needs to run as root"
echo "cannot set computer name to ${name} (${hname})"
exit 3
fi
if [[ "$name" != "" ]]; then
echo "Setting Computer name to ${name}"
jamf_binary="/usr/local/bin/jamf"
if [[ -x "${jamf_binary}" ]]; then
"${jamf_binary}" setComputerName -name "${name}"
"${jamf_binary}" recon
else
scutil --set ComputerName "${name}"
scutil --set HostName "${hname}"
scutil --set LocalHostName "${hname}"
fi
else
echo "could not determine computer name, exiting"
exit 4
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment