Posted on 08-20-2019 01:50 AM
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?
Solved! Go to Solution.
Posted on 08-20-2019 04:19 AM
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)}'
Posted on 08-20-2019 04:39 AM
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 :-)
Posted on 08-20-2019 02:29 AM
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
Posted on 08-20-2019 03:45 AM
@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?
Posted on 08-20-2019 04:17 AM
can try this:
computerName=$(dscl . read /Users/$getUser RealName | awk '{print $1 $2}' | awk 'FNR==2' | awk '{print tolower($1)}')
Posted on 08-20-2019 04:19 AM
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)}'
Posted on 08-20-2019 04:37 AM
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
Posted on 08-20-2019 04:39 AM
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 :-)
Posted on 08-20-2019 04:39 AM
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
Posted on 08-20-2019 04:42 AM
@jcar You are correct, and I don't intend to leave it this way.
Posted on 01-28-2020 06:24 AM
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.
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.