DeviceName, HostName & Local HostName

yjian
New Contributor III

Hi, I tried using JAMF school to set the names for all new devices. 

 

Under device placeholder I updated "Set Device Name" in JAMF and the Macbook device Computer Name did changed but when I use terminal to check hostname is it different. Is there a way to change all names at once?

Thank you

 

Screenshot 2023-05-16 at 22.55.34.png

3 REPLIES 3

AJPinto
Honored Contributor II

I use JAMF Pro if that matters for this. I do this with a script and the script runs on recurring check in targeting a smart group of devices who's hostname does not match their serial number.

 

#!/usr/bin/env bash
#*=============================================================================
#* Script Name: Configuration: Set Hostname
#* Created: 
#* Author: 
#*=============================================================================
#* Purpose: Requests input and renames computer based on Naming Convention
#*=============================================================================

#*=============================================================================
#* REVISION HISTORY
#*=============================================================================
#* Date:
#* Author: 
#* Issue: 
#* Solution: N/A
#*=============================================================================


#*=============================================================================
#* GLOBAL VARIABLES
#*=============================================================================
DIV1='echo ####################################################################'
DIV2='echo --------------------------------------------------------------------'
DIV3='echo ....................................................................'
ActiveUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }' | tr "[a-z]" "[A-Z]"`
ActiveUserRealName=`dscl . -read /Users/$ActiveUser | grep RealName: | cut -c11-`
    if [[ -z $ActiveUserRealName ]]; then
        ActiveUserRealName=`dscl . -read /Users/$ActiveUser | awk '/^RealName:/,/^RecordName:/' | sed -n 2p | cut -c 2-`
    fi
computerName=$(scutil --get ComputerName)
hostName=$(scutil --get HostName)
localHost=$(scutil --get LocalHostName)
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"')
#*=============================================================================
#* FUNCTIONS
#*=============================================================================
userInfo () {
    echo; $DIV1
    echo "User Information:"
    if [[ "$ActiveUserRealName" == "$ActiveUser" ]]; then
        echo  "$ActiveUserRealName (Local Admin)"
    else 
        echo "$ActiveUserRealName ($ActiveUser)"
    fi
    $DIV1
}
#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
userInfo

## Check & Update Computer Name
if [ "$computerName" == "$serialNumber" ]
then
	echo "Computer name matches serial number, $serialNumber"
else
	echo "Current Computer Name: $computerName"
	echo "Computer Name does not meet standards"
	echo "Changing Computer Name to match Serial Number"
	scutil --set ComputerName "$serialNumber"	
fi; $DIV2

## Check & Update Host Name
if [ "$hostName" == "$serialNumber" ]
then
	echo "Host Name matches serial number, $serialNumber"
else
	echo "Current Host Name: $hostName"
	echo "Host Name does not meet standards"
	echo "Changing Host Name to match Serial Number"
	scutil --set HostName "$serialNumber"
	
fi; $DIV2


## Check & Update Local Host
if [ "$localHost" == "$serialNumber" ]
then
	echo "Local Host matches serial number, $serialNumber"
else
	echo "Current Local Host: $localHost"
	echo "Local Host does not meet standards"
	echo "Changing Local Host to match Serial Number"
	scutil --set LocalHostName "$serialNumber"
	
fi; $DIV2


## Final Check
computerNameII=$(scutil --get ComputerName)
hostNameII=$(scutil --get HostName)
localHostII=$(scutil --get LocalHostName)

echo ""
echo "Serial number: $serialNumber"
echo "Computer Name: $computerNameII"
echo "Host Name: $hostNameII"
echo "Local Host: $localHostII"
if [[ "$computerNameII" == "$serialNumber" ]] && [[ "$hostNameII" == "$serialNumber" ]] && [[ "$localHostII" == "$serialNumber" ]] 
then 
	echo "Computer Name satisfies naming standards"
	$DIV1; exit 0
else
	echo "Computer does not meet naming standards"
	echo "More troubleshooting will be necessary."
    $DIV1; exit 1
fi 

## Perform final Recon to update computer name on Jamf
sudo jamf recon
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================

 

yjian
New Contributor III

Thank you for the script.

Is it possible for it to lookup to a Google Spreadsheet it will get the serial number and set devices name individually? e.g: serial 12345 > Air1 and 6789 >Air2 

yjian
New Contributor III

Thank you for the script.

Is there a way, if i have 100 MacBook and a google sheet of all serial number and desire name for each machine. 

Can I call a lookup to Google sheet and base on serial number it can change all the devices automatically.

Thank you