This is a script that gives a specific prefix for Location, adds Dept, then adds an identifier. It's fairly specific but the bash script for scraping the API response is good to use on anything you want to retrieve from JAMF. I currently have the identifier return a 3 digits to align to our naming conventions, but feel free to change it.
I apologize for my bash scripting format... not my primary language.
I set this to run on enrollment, but it can actually continually be run and sets the same name.
DEP Setup: After you associate computer to JAMF, I made several enrollment profiles and just filled in the department and location areas. This allows you to have the purchaser set everything before it even arrives. However, this is the only real manual setup you need to do and could be part of the ordering process. The Policy itself it set to run after enrollment.
1#!/bin/bash23MACADDRESS=$(networksetup -getmacaddress en0 | awk '{ print $3 }')4JSS=https://yourcompany.jamfcloud.com:4435API_USER=api6API_PASS=password789## Get JAMF XML10XML=$( curl -H "Accept:text/xml" -ksu $API_USER:$API_PASS $JSS/JSSResource/computers/macaddress/$MACADDRESS -X GET )1112##Find ID13#String to search14STRI="<id>"15#Find Position in string16IDPOS=${XML%%$STRI*}17IDPOSNUM=${#IDPOS}18#Find length of search variable19STRLNGT=${#STRI}20#Start after Tag21IDFPOS=`echo "$STRLNGT + $IDPOSNUM" | bc`22#Terminate string Length23IDFPOST=`echo "$STRLNGT + $IDPOSNUM + 10" | bc`24#Grab String and cut excess25ID=`echo ${XML:$IDFPOS:$IDFPOST} | cut -d "<" -f 1`2627##Find Department28#String to search29STRD="<department>"30#Find Position in string31DPTPOS=${XML%%$STRD*}32DPTPOSNUM=${#DPTPOS}33#Find length of search variable34STRLNGTD=${#STRD}35#Start after Tag36DPTFPOS=`echo "$STRLNGTD + $DPTPOSNUM" | bc`37#terminate string length38DPTFPOST=`echo "$STRLNGTD + $DPTPOSNUM + 10" | bc`39#Grab string and cut excess40DEPT=`echo ${XML:$DPTFPOS:$DPTFPOST} | cut -d "<" -f 1`4142##Find Location43#String to search44STRL="<building>"45#Find Position in string46LOCPOS=${XML%%$STRL*}47LOCPOSNUM=${#LOCPOS}48#Find length of search variable49STRLNGTL=${#STRL}50#Start after Tag51LOCFPOS=`echo "$STRLNGTL + $LOCPOSNUM" | bc`52#terminate string length53LOCFPOST=`echo "$STRLNGTL + $LOCPOSNUM + 10" | bc`54#Grab string and cut excess55LOCATION=`echo ${XML:$LOCFPOS:$LOCFPOST} | cut -d "<" -f 1`565758#Find if it's a laptop59IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book"`60#find ID length61idlength=`echo ${#ID}`6263#Add Location64if [ "$LOCATION" = "Home Office" ]; then65 PREFIX=HO66else67 echo "Unknown Location"68 exit 169fi7071#Determine Laptop or Not72if [ "$IS_LAPTOP" != "" ]; then73 PREFIX=$PREFIX"L"74else75 PREFIX=$PREFIX"D"76fi7778#Sort Department Naming79if [ "$DEPT" = "IT" ]; then80 PREFIX=$PREFIX"777"81elif [ "$DEPT" = "Marketing" ]; then82 PREFIX=$PREFIX"666"83elif [ "$DEPT" = "Web" ]; then84 PREFIX=$PREFIX"555"85elif [ "$DEPT" = "Private Label" ]; then86 PREFIX=$PREFIX"444"87else88 PREFIX=$PREFIX"XXX"89fi9091#format ID to 3 characters92if [ $idlength -gt 3 ]; then93 ID=echo ${ID: -3}94 Name=$PREFIX$ID95elif [ $idlength -lt 3 ]; then96 ID="0"$ID97 Name=$PREFIX$ID98else99 Name=$PREFIX$ID100fi101102103#set name to match104/usr/sbin/scutil --set ComputerName "$Name"105/usr/sbin/scutil --set LocalHostName "$Name"106/usr/sbin/scutil --set HostName "$Name"107108#Flush naming cache109dscacheutil -flushcache110111#Report to Jamf112jamf recon113114exit 0
Hope that helps people with similar formats.
Edit: If your having issues with the API returns, I have it set to only move to 10 characters past where it sees the tag. You can adjust that by changing the 10 to whatever number you want. Technically I could put 100, but I wanted to run as efficient as possible.
