Rename Script issues on macOS Monterey

h_tran
New Contributor

Hello, I am having issues with the following script that worked great as a policy for Big Sur and older OS/Macs to rename Macs, but for some reason, Macs with M1/Monterey will not execute the rename commands as a Policy Script.

 

#!/bin/sh

/usr/sbin/scutil --set ComputerName "Test-Mac"
/usr/sbin/scutil --set HostName "Test-Mac"
/usr/sbin/scutil --set LocalHostName "Test-Mac"

exit 0

 

6 REPLIES 6

TCBIW
New Contributor III

We switched to using the jamf binary to rename and that has worked out for our M1 macs and Monterey just fine.

jamf setcomputername -useSerialNumber

 

You could do something similar:

jamf setcomputername -name "Test-Mac"

 

The jamf command will take care of all 3 scutil commands that you were manually running.

 

Maybe someone else has something better, but this has been working for me on M1 mac with 12.2

scottb
Honored Contributor

Yep, do this once a week per Mac and it works great.  One line, all three names changed.

You can use any of the Apple System Information you wish to and it seems pretty foolproof.

ubcoit
Contributor II

This has been working for us without issue to date.

/usr/sbin/scutil --set HostName "$serialNumber"
sleep 3
/usr/sbin/scutil --set LocalHostName "$serialNumber"
sleep 3
/usr/sbin/scutil --set ComputerName "$serialNumber"
sleep 3
/usr/bin/dscacheutil -flushcache

cbrewer
Valued Contributor II

I have also found a pause is needed between scutil set computer name commands on Monterey. A sleep 1 between each one is getting the job done for us.

TS6845
New Contributor

what's the general though on this one? It launches a rename box, type in the name and its set. And yes its three different scripts I've found over time on the jamf forums and crammed into one.  Working for me.. set it as a self service policy. 

 

#!/bin/sh

# Get serial number
serialNumber=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`

# Set name to serial number (in case name is not set by user)
jamf setcomputername -name "$serialNumber"

/usr/bin/dscacheutil -flushcache

# Get currently logged in user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

# Get the logged in UID
loggedInUID=$(id -u $loggedInUser)

# Prompt for Computer Name as the user
/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser whoami
computerName=$(/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOL
tell application "System Events"
activate
with timeout of 60 seconds
set answer to text returned of (display dialog "Set Computer Name" with title "MyOrganization" default answer "$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')")
end timeout
end tell
EOL)
# Check to make sure $computerName is set
if [[ -z $computerName ]]; then
echo "Computer Name not set. Exiting..."
exit 0
fi
# Set name using variable created above
computerName=`echo $computerName | tr '[:lower:]' '[:upper:]'`
jamf setcomputername -name "$computerName"

/usr/bin/dscacheutil -flushcache

echo "Computer Name set to $computerName"
# Confirm Computer Name
/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOL
tell application "System Events"
activate
display dialog "Computer Name set to " & host name of (system info) buttons {"OK"} default button 1 with title "MyOrganization" giving up after 15
end tell
EOL

Mithrandir
New Contributor III

Here's mine (I've taken bits and pieces from others' scripts modifying for my environment):

#!/bin/bash -v

MACADDRESS=$(networksetup -getmacaddress en0 | awk '{ print $3 }')
JSS=https://[your JSS]:443
API_USER=
API_PASS=
ASSET_TAG_INFO=$(curl -k $JSS/JSSResource/computers/macaddress/$MACADDRESS --user $API_USER:$API_PASS | xmllint --xpath '/computer/general/asset_tag/text()' -)
SERIAL_NUMBER=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
currentmodel=$( /usr/sbin/system_profiler SPHardwareDataType | grep "Model Name" | grep "MacBook" )

if [[ $currentmodel == *'MacBook'* ]];
then
model="L"
else
model="D"
fi

if [ -n "$ASSET_TAG_INFO" ]; then
echo "Processing new name for this client..."
echo "Changing name..."
/usr/sbin/scutil --set HostName MDM-"$ASSET_TAG_INFO"-$model
/usr/sbin/scutil --set ComputerName MDM-"$ASSET_TAG_INFO"-$model
/usr/sbin/scutil --set LocalHostName MDM-"$ASSET_TAG_INFO"-$model
echo "Name change complete. (MDM-$ASSET_TAG_INFO-$model)"
jamf recon --verbose


else
echo "Asset Tag information was unavailable. Using Serial Number instead."
echo "Changing Name..."
/usr/sbin/scutil --set HostName MDM-$SERIAL_NUMBER-$model
/usr/sbin/scutil --set ComputerName MDM-$SERIAL_NUMBER-$model
/usr/sbin/scutil --set LocalHostName MDM-$SERIAL_NUMBER-$model
echo "Name Change Complete (MDM-$SERIAL_NUMBER-$model)"
jamf recon -verbose

fi

 

Had to change the first grep statement in currentmodel declaration statement from "Model Identifier" to "Model Designation"