Auto Mac Set Name Script fails on Monterey 12.3 and up




Below is a script I have been using with success on older versions of macOS however, it stopped working on monetary. 


The script pulls the current username from "ls -l /dev/console | cut -d " " -f 4" and automatically sets the ComputerName, HostName, and LocalHostName as the current username. 


Any thoughts on how to get this working again on this version of MacOS? Thanks, ~ B


##### Script 



username=`ls -l /dev/console | cut -d " " -f 4`

scutil --set ComputerName "$4""$username"
scutil --set HostName "$4""$username".<domain>
scutil --set LocalHostName "$4""$username"

jamf recon




Script exit code: 2
Script result: /Library/Application Support/JAMF/tmp/network name auto: line 8: syntax error near unexpected token `newline'
/Library/Application Support/JAMF/tmp/network name auto: line 8: `scutil --set HostName "$4""$username".<domain>'
Error running script: return code was 2.

New Contributor II

Maybe something like this would work?


user_name=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }')
/usr/sbin/scutil --set HostName "$user_name".domain
/usr/sbin/scutil --set LocalHostName "$user_name"
/usr/sbin/scutil --set ComputerName "$user_name"


Contributor III

Hi @bcrockett , I am using this script to rename the client name and it works well:

macowner=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
if ! [ $macowner ]; then
echo "cannot detect current user, cannot set clientname" && exit
else echo "settig clientname to MACvon-$macowner" && sudo jamf SetComputerName -name MACvon-$macowner

Thanks, @NOVELLUS I will try that out. ~B

New Contributor II

Oh @bcrockett, you didn't like my script? I wrote it especially for you 😿

@user-YshbQMtOGr  I tried it and did not see great results. It may be due to the JAMF Pro data tables not being updated after the policy with the script runs and waiting for JAMF recon. However, even with Jamf Recon added to it, there seems to be a noticeable lag. 

New Contributor II

Well thanks for giving it a shot. I thought it would work for sure! Hopefully @NOVELLUS's script works better.