Skip to main content
Question

Jamf Script to dynamically rename a Macbook


Forum|alt.badge.img+3

Hello,

First time posting here, just want to start by saying thank you to everyone for your contributions, I've scoured this forum for a long time.

My current issue that I haven't been able to find any solution for is dynamically renaming our Macbooks. The script I use currently is this:

#!/bin/sh

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

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

#set all the name in all the places
scutil --set ComputerName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"

#update Jamf
jamf recon

This works great, but the problem I have is that every laptop get's renamed with the LT1 tag. So when we replace a laptop for someone, we now have TWO LT1 laptops listed for that user in Jamf.

Is there some what I can modify my current script to be dynamic? Such as, it pulls data from Jamf, determines if there is already an lt1 listed for that user, and then changes it to lt2 instead?

If not, no worries, we'll just stick to manual intervention, but would love to have a way to automate this.  

Thanks all!

 

3 replies

ljcacioppo
Forum|alt.badge.img+17
  • Jamf Heroes
  • 183 replies
  • October 14, 2021

You would likely need to use the API to do that. Something like the following. I'm always hesitant to have any script include credentials though if running through jamf, so if doing so, you'd probably want to create a service account with very limited privileges and then use the base64 credentials for that account to run the script.

 

 

#!/bin/bash jamfURL='https://jamf-pro-url/JSSResource' name='$firstInitial$lastName"-lt1"' computerCheck=$( /usr/bin/curl -s \\ --header "authorization: Basic $base64creds" \\ --header "Accept: text/xml" \\ --request GET \\ $jamfURL/computers/name/$name | xmllint --xpath '/computer/general/name/text()' - 2> /dev/null ) if [[ ! -z $computerCheck ]];then computerName=$firstInitial$lastName"-lt2" fi

 

 

 


Forum|alt.badge.img+21
  • Valued Contributor
  • 322 replies
  • October 15, 2021

Just a reminder that personally identifiable information (PII) should never be used in a device name, especially for minor students.  Device names are broadcast in the clear on any associated network and could therefore be a security and safety risk for those users.

 

A better option is to generate a unique name based on a device attribute (e.g. asset tag, serial, MAC address).

 

/usr/local/bin/jamf setComputerName -name "Mac-$(ifconfig en0 | awk '/ether/{print $2}' | tr -d :’)"

Forum|alt.badge.img+2
  • Employee
  • 1 reply
  • October 17, 2021

I like any of the following for dynamic naming or combination thereof,

#!/bin/bash

sysInfo=($(ioreg -rd1 -c IOPlatformExpertDevice | grep -Ew 'IOPlatformUUID|IOPlatformSerialNumber|product-name' | awk '{print $3}'| tr -d '"'))

# ${sysInfo[0]} = UUID
# ${sysInfo[1]} = Model
# ${sysInfo[2]} = Serial number

UUID=$(echo ${sysInfo[0]})
MODEL=$(echo ${sysInfo[1]} | sed 's|[<>0-9,]||g')
SerialNumner=$(echo ${sysInfo[2]})

computerName="${SerialNumner}-${MODEL}"

echo /usr/sbin/scutil --set ComputerName ${computerName}
echo /usr/sbin/scutil --set LocalHostName ${computerName}
echo /usr/sbin/scutil --set HostName ${computerName}


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