Skip to main content
Jamf Nation, hosted by Jamf, is a knowledgeable community of Apple-focused admins and Jamf users. Join us in person at the ninth annual Jamf Nation User Conference (JNUC) this November for three days of learning, laughter and IT love.

script to rename computer name (letter of first name and last name)

I know there's a number of discussion on it but haven't quite found the correct one. I'm trying to have a policy that will call a script to change computer name based upon users first letter and last name. Here's the workflow:

-User will enroll via DEP. Enter in John Smith
-DEP will look for SplashBuddy (SB) to run.
-SB will do it's thing and will trigger all install packages.
-one of the policies to kick off is to take the information set for the User name and change computer name to jsmith-mbp on the local macbook.

Any help would be appreciated. Thank you

Like Comment
Order by:
SOLVED Posted: by boberito

So you'd have to write a script that does that. You could put that script potentially in a payload-free package or a script that runs with a policy.

#!/bin/sh

#gets current logged in user
getUser=$(ls -l /dev/console | awk '{ print $3 }')

#gets named
firstInitial=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($2)}' | cut -c 1)
lastName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($3)}')
computerName=$firstInitial$lastName"+mbp"

#set all the name in all the places
scutil --set ComputerName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"
Like
SOLVED Posted: by Asnyder

This is what I use with my DEP setup. Not that you can't use underscores "_" in the host name. This is why I have hostname and hostname2. This will also add the mac address at the end in case of duplicates. Occassionally at my school we have same first initial and last name so that prevents duplicates.

Just place the script in a policy and call it using

jamf policy -event yourtrigger
#!/bin/bash
##########################################################################################
# Computer Naming Script

#Set the hostname

# figure out the user
user=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')

#figure out the user's full name
name=$(finger "$user" | awk -F: '{ print $3 }' | head -n1 | sed 's/^ //' )

# get first initial
finitial="$(echo "$name" | head -c 1)"

# get last name
ln="$(echo "$name" | cut -d \  -f 2)"

#Get Mac
mac=`networksetup -getmacaddress Wi-Fi | awk '{ field = substr($3,10,8) }; END{ print field }' | sed s/://g`


# add first and last together
un=($ln"_"$finitial"_"$mac)

un2=($ln"-"$finitial"-"$mac)

# clean up un to have all lower case
hostname=$(echo "$un" | awk '{print tolower($0)}')
hostname2=$(echo "$un2" | awk '{print tolower($0)}')

echo $hostname

sudo scutil --set HostName $hostname2
sudo scutil --set LocalHostName $hostname2
sudo scutil --set ComputerName $hostname
dscacheutil -flushcache
Like
SOLVED Posted: by bburdeaux

We do something similar, but use another AD field to fill in the campus the person is at as well(mapped to the Department field in JamfPro), so we pull the info from the User and Location section using the API.

#!/bin/sh

jssUser=$4
jssPass=$5
jssHost=$6

serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

username=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${jssUser}:${jssPass}" "${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/location" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<username>/{print $3}')
dept=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${jssUser}:${jssPass}" "${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/location" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<department>/{print $3}')

if [ "$username" == "" ]; then
    echo "Error: Username field is blank."
    exit 1
elif [ "$dept" == "" ]; then
    echo "Error: Department field is blank."
    exit 1
else

compname=$dept"-"$username"-mac"

/usr/sbin/scutil --set HostName "$compname"
/usr/sbin/scutil --set LocalHostName "$compname"
/usr/sbin/scutil --set ComputerName "$compname"

fi
Like
SOLVED Posted: by tomatoes27

@boberito Thank you for the script. It looks good.

Like
SOLVED Posted: by RLim945

@boberito Great script! Would anyone know how to add to this script so that it can check the type of Mac it is and append the appropriate name?

For John Smith, we use:

JSMITH.PORTABLE for Laptops
JSMITH.DESKTOP for Desktops

Edit: Actually, I'm running into the following error

SCPreferencesSetLocalHostName() failed: Invalid argument

I believe it's because the + symbol is not an accepted character for the Local Host Name?

Like
SOLVED Posted: by boberito

No idea about that error. It's entirely possible the + isn't allowed, or maybe there's a hidden character like a return character or something. I dunno.

sysctl -n hw.model | cut -d "," -f 1 | tr -d '[0-9]_'

That'll give you the model of the computer

Then you'd need some if or case statements to determine if you want it named portable or desktop.

Like
SOLVED Posted: by boberito

And looking at the system preferences, sharing. You'll see the Computer Name has a + but the local network name right under it is the name without the plus.

Like
SOLVED Posted: by PeterClarke

I just use a "Smart List" with criteria: Model is like Macbook

( = Macbook, Macbook AIR, Macbook Pro ) = Laptop
Anything else is a Desktop…

So Smart Group: Desktop criteria: Model is not like Macbook

This seems like the simplest way to distinguish between Desktops and Laptops…

( unless you need the criteria in a script as part of some other processing… )

Like
SOLVED Posted: by tcarlson

I was able to use boberito's script, at the top, I edited to display/change computer name as firstname lastname. Not a huge issue but the names are lower case, is there a tweak so that I can get the names displayed first letter is capitalized. Thanks in advance.

Like
SOLVED Posted: by OOBIDUB

Something like this should work.

firstInitial=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print toupper($2)}' | cut -c 1)

Like
SOLVED Posted: by steagle

@tcarlson - what does your script look like now, either with or without proper capitalization (ideally with)? I'm trying to accomplish the same thing.

Like