Skip to main content
Solved

Help needed with a computer name script.


Forum|alt.badge.img+3

I need help with a script which can pull the first and last name of a user from the device and set this as their computer name, which is then reflected in Jamf Pro.

I've had some success with the below script taken from another thread, but this only works for users with one first and last name. When someone with a middle name adds it to their account name, it doesn't add the surname, rather the first two names.

#!/bin/sh

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

#gets named
firstName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($2)}')
lastName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($3)}')
computerName=$firstName$lastName

#set all the name in all the places
scutil --set ComputerName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder && sudo jamf recon

Does anyone have a current script which does this reliably or something very similar?

Best answer by AHolmdahl

Just add | sed 's/ //g' | awk '{print tolower($0)}'
I.e. computerName=$(id -P $(stat -f%Su /dev/console) | cut -d : -f 8 | sed 's/ //g' | awk '{print tolower($0)}'

View original

Forum|alt.badge.img+3
  • New Contributor
  • August 20, 2019

HI Neil,

Could you try to run this command :

computerName=$(id -P $(stat -f%Su /dev/console) | cut -d : -f 8)

and check if you have all needed information.

Thanks,

Hasan


Forum|alt.badge.img+3
  • New Contributor
  • August 20, 2019

@hsert thank you, this looks like it can work.

How can I get that computerName command to remove the spaces and potentially make everything lowercase?


DBrowning
Forum|alt.badge.img+24
  • Esteemed Contributor
  • August 20, 2019

can try this:

computerName=$(dscl . read /Users/$getUser RealName | awk '{print $1 $2}' | awk 'FNR==2' | awk '{print tolower($1)}')

Forum|alt.badge.img+6
  • Contributor
  • August 20, 2019

Just add | sed 's/ //g' | awk '{print tolower($0)}'
I.e. computerName=$(id -P $(stat -f%Su /dev/console) | cut -d : -f 8 | sed 's/ //g' | awk '{print tolower($0)}'


Forum|alt.badge.img+21
  • Valued Contributor
  • August 20, 2019

Neil,

I hate to rain on this thread, but I'll just add that if you are in education (more K12 than HiEd), you really should NOT use actual names as device names. Since hostnames are broadcast in the clear on any network, a device name could be used to determine when a particular student is in a particular location. This is a security risk and could potentially be a HUGE safety risk.

Probably not an issue in enterprise though.

Just my $0.02


Forum|alt.badge.img+3
  • New Contributor
  • August 20, 2019

yeap more like AHolmdahl

computerName=$(id -P $(stat -f%Su /dev/console) | cut -d : -f 8 | sed 's/ //g' | awk '{print tolower($0)}')
He only forgot a bracket at the end :-)


Forum|alt.badge.img+3
  • New Contributor
  • August 20, 2019

Thank you everyone! With you help I could put all the pieces together :)

#!/bin/sh

#gets named
fullName=$(id -P $(stat -f%Su /dev/console) | cut -d : -f 8 | sed 's/ //g' | awk '{print tolower($0)}')
computerName=$fullName

#set all the name in all the places
scutil --set ComputerName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder && sudo jamf recon

Forum|alt.badge.img+3
  • New Contributor
  • August 20, 2019

@jcar You are correct, and I don't intend to leave it this way.


Forum|alt.badge.img+4
  • Contributor
  • January 28, 2020

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings