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
@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!
@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
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
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
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
That works great!! Thank you!!
@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
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”
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
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
@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 @Mauricio11, your script works like a charm!
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?