Script to rename Mac fails with error

New Contributor II

Greetings and salutations!


I have a script that displays a prompt to rename the Mac during provisioning to my org's naming convention.  It was working until relatively recently and the policy logs show it fails with "Script result: 69:189: execution error: Application isn’t running. (-600)".  my guess is some Apple update broke the script but scripting is a weaker part of my skills so help would be fantastic.  Thanks!



CurrentUser=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')
MacName=$(sudo -u "$CurrentUser" /usr/bin/osascript -e 'tell application "System Events" to set MacName to text returned of (display dialog "Enter the new Computer Name:" buttons "Continue" default button "Continue" default answer "" with icon 1)')

scutil --set ComputerName "$MacName"
scutil --set HostName "$MacName"
scutil --set LocalHostName "$MacName"
defaults write /Library/Preferences/SystemConfiguration/ NetBIOSName -string "$MacName"

/usr/bin/dscacheutil -flushcache




I was able to run this without issue from Jamf and from terminal directly - terminal did ask for automation access to system events, but that was it. I added an echo so I knew what it was named in the log.  Give a try to passing a PPPC profile for terminal to allow system events, it may do the trick.

Thanks for the reply!  I get the same automation access prompt which in the past was a good indicator the script was working but now the name just stays at "accountname's Macbook Pro" instead of the name I entered.  Forgive the dumb question but how would I implement your suggestion?  

Even after a restart? Also, is this just showing "accountname's Macbook Pro" in jss gui or on the actual mac? perhaps a recon then restart would allow it to show properly. Another thing to check would be if your macs are bound to AD. Perhaps disjoin then rename?

It shows that in both places.  Macs are not bound to AD in my environment.

I should mention also that a restart is the last step of the policy run.

The name of the computer will remain the same in Jamf Pro until you do a recon to update the record.  when you run the script from Jamf Pro, configure the update inventory payload in the policy to get it to update.  within the script, I just added the following to the end so in the log it shows the name that was set at the very least.

echo "computer has been named $MacName"

 to configure a PPPC payload for I would suggest you use the PPPC utility  to find the IDs of apps etc to allow for actions to be used etc.

Valued Contributor

I gave a script in that is working via a self service.  It's one of many policies that chain together and set up a computer via a self service item that fires off the trigger.  Those 3 locations that you set using scutil can be done with the one call to jamf setComputerName -name.  Not sure about whether it sets the NetBIOSName but mine appears correct

Valued Contributor II

Why not just have a script that renames the Mac without prompting? Here's what I came up with about a year ago. You can easily modify this to do what you need it to do. I also created a version that uses the Mac's serial number as part of the computer name. I'm including both versions below.

The first script uses a naming scheme I came up with that uses my client company's initials at the beginning of the computer name. The second one uses the Mac's serial number and appends the user's name. I made the version with the serial number to help someone else but I advised him that using the user's name in the computer name is risky if they're going to be on a public WiFi. 


#Rename the Mac using the first and last initial of the user's name. Before using this script, replace "XX" in line 12 with the client company's initials

#Who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

#Generate computer name - Replace "XX" with the client company's initials
firstInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($2)}' | cut -c 1)
lastInitials=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($3)}' | cut -c 1,2,3)

echo $computerName

#Set the computer name. Wait 5 seconds after each command.
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName

#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon



### Names the Mac using the user's fist and last name in all caps followed by the serial number separated by dashes.

#What is the serial number of the computer?
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')

#who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

#What is the first name of the user?
firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }')

#What is the last name of the user?
lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }')

#Convert firstName and lastName to all caps
firstNameCAP=$(echo $firstName | tr '[a-z]' '[A-Z]')
lastNameCAP=$(echo $lastName | tr '[a-z]' '[A-Z]')

#Genrate computer name
echo $computerName

#Set the computer name.
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName

#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon

I have used a version of this script in my policy array that runs through DEPNotify. It works great every time. I have another version that can be used in a policy that allows us to fill in the values we want so that we can rename a Mac using a policy at check-in.

Thanks for providing these!  We need to manually rename our Macs to comply with our naming convention which is username-assettag#.  That 2nd version you mention might be the one I need. 

I'm happy I was able to help!