script to change computer name

jp2019
New Contributor III

I need help with a script to change computer name. I am using the DEPNotify Starter 2.0.1 script. I am using the Popup 3 label for the list of University Campus, Popup 1 label for the building, and would like to use the last seven of the serial number. If Campus is New York, and the building is Central Hall and the serial number of machine is 1234567, I would like that translated to NYCH1234567

I am currently using the script as a policy in DEPNotify policy array with a custom trigger. I would like to make it that as the User/Tech selects their Campus and the Building the device is assigned to, that it will use that as part of the computer naming along with the 7 digits of the serial number.

!/bin/sh
set serial variable
serial=/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'
appendserial=echo "${serial:5}"
fordhamname=APPLE${appendserial}
/usr/sbin/scutil --set ComputerName "${apple}"
/usr/sbin/scutil --set LocalHostName "${apple}"
/usr/sbin/scutil --set HostName "${apple}"
exit 0

Can anyone make any suggestions, or like previous requests, does anyone have anything similar to what I am looking to do?
I would really appreciate the help.

2 ACCEPTED SOLUTIONS

hdsreid
Contributor III

Not sure if you have access to this information, but I have the location settings automatically set by the Jamf Pro server based on the IP it enrolls with. I have created network segments to match buildings to subnets.

once that is taken care of, I just name them based on serial and assign the current logged in user as the user to be uploaded to Jamf Pro inventory. Not quite what you are trying to do, but here is the gist of what I'm doing in the hope it gives you some guidance or ideas:

#!/bin/bash
loggedInUser=$(/usr/bin/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 + "
");')
sn=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn

# Set in JAMF Binary

/usr/local/bin/jamf setComputerName -name $sn
/usr/local/bin/jamf recon -endUsername $loggedInUser

i strive to make mine more automated than not, as most of my users are not very computer savvy to say the least

View solution in original post

Mauricio
Contributor III

@bhejmady239 

A possible option but you will need to define a name for the non MBs.

#!/bin/bash

userName=$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)
macModel=$(/usr/sbin/sysctl -n hw.model)

#  Need to define the non MacBooks options
if [[ "$macModel" =~ BookAir ]]; then
    setModel='MBA'
  elif [[ "$macModel" =~ BookPro ]]; then
    setModel='MBP'
  elif [[ "$macModel" =~ Book ]]; then
    setModel='MB'
else
    setModel='DS'
fi

computerName="$userName-$setModel"

# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"

exit 0

 

View solution in original post

12 REPLIES 12

hdsreid
Contributor III

Not sure if you have access to this information, but I have the location settings automatically set by the Jamf Pro server based on the IP it enrolls with. I have created network segments to match buildings to subnets.

once that is taken care of, I just name them based on serial and assign the current logged in user as the user to be uploaded to Jamf Pro inventory. Not quite what you are trying to do, but here is the gist of what I'm doing in the hope it gives you some guidance or ideas:

#!/bin/bash
loggedInUser=$(/usr/bin/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 + "
");')
sn=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn

# Set in JAMF Binary

/usr/local/bin/jamf setComputerName -name $sn
/usr/local/bin/jamf recon -endUsername $loggedInUser

i strive to make mine more automated than not, as most of my users are not very computer savvy to say the least

J_Martinez
New Contributor III

@hdsreid

This script is a great help . We name our computers based on department and users initials. Is there any way to modify the script to prompt the user for first, last name and department then use that information to build out the computer name ie “MarketingJM”

Thanks!

jcostanzo
New Contributor

@hdsreid Thanks so much for posting this script, I was getting frustrated as machines were renaming or enrolling with generic "Macbook Pro," now I can align everything!

Cheers,
Joe

aledesma
New Contributor II

is there a way to add an apostrophe s to the end of the username?

trying something similar to rename our user's computer names in Jamf.  But instead  of it being "UserName MacbookModel" 

I'm trying to make it so that in jamf it displays as "UserName's MacbookModel"

this is what I have so far, but dont know really how to add the 's to the user name.... not too good with scripting, my apologies

#!/bin/bash

UserName=$(ls -la /dev/console | cut -d " " -f 4)
MacbookModel=$(sysctl -n hw.model | cut -d "," -f1)

scutil --set ComputerName $UserName $MacbookModel




@aledesma 

A possible way... 

#!/bin/bash

UserName=(ls -la /dev/console | cut -d " " -f 4)
MacbookModel=(sysctl -n hw.model | cut -d "," -f1)
computerName=("$UserName's $MacbookModel")

scutil --set ComputerName "$computerName"

Regards

aledesma
New Contributor II

That works great!! Thank you!!

Is there any possible way to rename in the below format.

If the model is MacBook Pro hostname should be “username-MBP”

if the model is MacBook Air hostname should be “username-MBA”

bhejmady239
New Contributor II

Is there any possible way to rename in the below format.

If the model is MacBook Pro hostname should be “username-MBP”

if the model is MacBook Air hostname should be “username-MBA”

Mauricio
Contributor III

@bhejmady239 

A possible option but you will need to define a name for the non MBs.

#!/bin/bash

userName=$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)
macModel=$(/usr/sbin/sysctl -n hw.model)

#  Need to define the non MacBooks options
if [[ "$macModel" =~ BookAir ]]; then
    setModel='MBA'
  elif [[ "$macModel" =~ BookPro ]]; then
    setModel='MBP'
  elif [[ "$macModel" =~ Book ]]; then
    setModel='MB'
else
    setModel='DS'
fi

computerName="$userName-$setModel"

# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"

exit 0

 

Thank you @Mauricio, your script works like a charm!

Musicmaker
Contributor

I am using the following script which runs during enrollment:

#!/bin/zsh

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

# Set name to serial number (in case name is not set by user)
scutil --set ComputerName "$serialNumber"
sleep 1
scutil --set LocalHostName "$serialNumber"
sleep 1
scutil --set HostName "$serialNumber"
sleep 1

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

# Only proceed if _mbsetupuser is logged in (used by Apple for setup screens)
if [[ ! $loggedInUser = "_mbsetupuser" ]]; then
  echo "Logged in user is not _mbsetupuser. Exiting..."
  exit 0
fi

# 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<<EOF
set answer to text returned of (display dialog "Set Computer Name" with title "Company Name" default answer "$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')" giving up after 900)
EOF
)

# 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:]')
scutil --set ComputerName "$computerName"
sleep 1
scutil --set LocalHostName "$computerName"
sleep 1
scutil --set HostName "$computerName"
sleep 1

echo "Computer Name set to $computerName"

# Confirm Computer Name
/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOF
display dialog "Computer Name set to " & host name of (system info) buttons {"OK"} default button 1 with title "Company name" giving up after 5
EOF

During enrollment you're prompted with a window to fill in the right computername.
When I enter the name and press ok it als confirms that is has changed the computername.

But when enrollment is complet and I'm logged in to macOS the hostname is "MacBook Pro" and not the name I entered. What do am I missing?