Posted on 10-07-2022 02:01 PM
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!
#!/bin/zsh
defaults=/usr/bin/defaults
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/com.apple.smb.server NetBIOSName -string "$MacName"
/usr/bin/dscacheutil -flushcache
Posted on 10-07-2022 03:17 PM
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.
Posted on 10-10-2022 05:59 AM
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?
Posted on 10-10-2022 08:31 AM
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?
Posted on 10-10-2022 08:41 AM
It shows that in both places. Macs are not bound to AD in my environment.
Posted on 10-10-2022 08:52 AM
I should mention also that a restart is the last step of the policy run.
Posted on 10-10-2022 08:34 AM
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.
Posted on 10-11-2022 08:55 PM
I gave a script in https://community.jamf.com/t5/jamf-pro/script-to-rename-mac/m-p/143210 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
Posted on 10-12-2022 08:37 AM
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.
#!/bin/sh
#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)
computerName=$"XX-M-$firstInitial$lastInitials"
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
#!/bin/sh
### 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
computerName=$firstNameCAP-$lastNameCAP-$serialNumber
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.
Posted on 10-12-2022 08:54 AM
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.
Posted on 10-12-2022 08:56 AM
I'm happy I was able to help!