Using the Jamf Pro agent to set computer name to match the Mac’s hardware serial number on macOS Sonoma
2024-8-7 23:25:48 Author: derflounder.wordpress.com(查看原文) 阅读量:16 收藏

Home > Jamf Pro, macOS, Scripting > Using the Jamf Pro agent to set computer name to match the Mac’s hardware serial number on macOS Sonoma

Using the Jamf Pro agent to set computer name to match the Mac’s hardware serial number on macOS Sonoma

In a number of environments, Mac admins have chosen to use the Mac’s hardware serial number when naming the computer’s hostname (otherwise referred to as the computer name.) This is a task which the Jamf Pro agent includes built-in functionality for, using the following command:


For more information on this functionality, please run the following command on a Mac with the Jamf Pro agent installed:


You should see the help information which is relevant to this command:


username@computername ~ % jamf help setComputerName
Usage: jamf setComputerName [-target <target volume>] [-name <name>]
[-useMACAddress] [-useSerialNumber] [-suffix <suffix>]
[-prefix <prefix>] [-fromFile <path to file>]
-target The target drive to set the name on
-name The new name for the computer
-useMACAddress Generate the name using the MAC Address
-useSerialNumber Generate the name using the Serial Number
-prefix Add this prefix to the MAC Address or Serial Number
-suffix Add this suffix to the MAC Address or Serial Number
-fromFile The path to a CSV file containing the computer's MAC Address or Serial Number followed by the desired name
username@computername ~ %

You can also use the scutil command line tool for this task, if you don’t have the Jamf Pro agent installed. However, for the scutil tool, you would need to run the following commands to match the functionality as provided by the Jamf agent command listed above:


To have the serial number information be provided by the system, the following commands can be used:


For more information, please see below the jump.

To put all of what I discussed above together, I’ve written the following script which does the following:

  1. Checks if the Jamf agent is installed and set as executable.
  2. If the Jamf agent is installed and set as executable, the Jamf agent is used to set the hostname to match the Mac’s serial number.
  3. If the Jamf agent is not installed or not set as executable, the scutil command line tool is used to set the hostname to match the Mac’s serial number.

This script is available below and also available on GitHub via the following link:

https://github.com/rtrouton/rtrouton_scripts/tree/main/rtrouton_scripts/set_computer_name_to_match_machine_serial_number


#!/bin/bash
# Sets the computer name to the machine's serial number.
#
# If the Jamf agent is installed, the script uses the Jamf agent to set
# the computer name to the machine's serial number.
#
# If the Jamf agent is not installed, the scutil command line tool is used.
jamfAgentPath="/usr/local/jamf/bin/jamf"
if [[ -x "$jamfAgentPath" ]]; then
"$jamfAgentPath" setComputerName -useSerialNumber
else
machineSerial=$(/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ { print $4; }')
if [[ -n "$machineSerial" ]]; then
/usr/sbin/scutil –set ComputerName "$machineSerial"
/usr/sbin/scutil –set LocalHostName "$machineSerial"
/usr/sbin/scutil –set HostName "$machineSerial"
fi
fi
exit 0

文章来源: https://derflounder.wordpress.com/2024/08/07/using-the-jamf-pro-agent-to-set-computer-name-to-match-the-macs-hardware-serial-number-on-macos-sonoma/
如有侵权请联系:admin#unsafe.sh