Can a script query a machine type and parse it into a 3 character text?

jeff_kunkel
New Contributor

Our computers are named by 2 digit country, 3 digit city, 3 digit computer model, and username. Ex.

us-nyc-mbp-firstnamelastname

I have this computer rename script which runs great. Right now, the two user inputs are Location and Machine Model. I am pretty sure I can have the script query the system profiler, and change that into the machine code rather than the input from the user. Any thoughts on how to go about adding that in (more accurately replacing the section that asks the user what machine they are using)?

#!/bin/bash
# get currently logged in user
currentUser=$( /usr/bin/stat -f "%Su" /dev/console )
echo "Current user is $currentUser"
#gets named
macName=$currentUser
echo "Formatted username for MacBook is $macName"
# prompt current user to choose a site
theCommand='choose from list {"Atlanta, GA (ATL)", "Chicago, IL (CHI)", "Fort Lauderdale, FL (FLL)", "Los Angeles, CA (LAX)", "Miami, FL (MIA)", "New York, NY (NYC)", "San Francisco, CA (SFO)", "Seattle, WA (SEA)", "Virginia (VA)", "Washington, DC (WDC)"} with title "Common Living IT" with prompt "Select the Common site you report to:" multiple selections allowed false empty selection allowed false'
chosenSite=$( /bin/launchctl asuser "$currentUser" sudo -iu "$currentUser" /usr/bin/osascript -e "$theCommand" )
# if the current user cancels the dialog, stop
if [ "$chosenSite" = "false" ]; then
    echo "Choose site prompt canceled. Stopping script."
    exit 1
fi
siteCode=$( /usr/bin/awk -F "[()]" ' { print (tolower($2)) } ' <<< "$chosenSite" )
echo "Choosing site "$chosenSite""
# set computer model
theCommand='choose from list {"MacBook Air (MBA)", "MacBook Pro (MBP)", "Mac Mini (MM)", "iMac (IMC)"} with title "Common Living IT" with prompt "Select the device model you are using:" multiple selections allowed false empty selection allowed false'
modelType=$( /bin/launchctl asuser "$currentUser" sudo -iu "$currentUser" /usr/bin/osascript -e "$theCommand" )
modelCode=$( /usr/bin/awk -F "[()]" '{ print (tolower($2)) } ' <<< "$modelType" )
# if the current user cancels the dialog, stop
if [ "$modelType" = "false" ]; then
    echo "Model Type canceled. Stopping script."
    exit 1
fi
echo "Choosing device "$modelCode""
# set computer name
/usr/local/jamf/bin/jamf setComputerName -name "us-${siteCode}-${modelCode}-${macName}" -target /
echo "Setting ComputerName to us-${siteCode}-${modelType}-${macName}"
# update Jamf Pro inventory with new name - do not use current policy's update inventory method
echo "Updating Jamf Pro inventory"
/usr/local/jamf/bin/jamf recon
exit 0
0 REPLIES 0