Skip to main content
Solved

Trying to find the best way to rename computers after enrollment

  • September 16, 2021
  • 44 replies
  • 302 views

Show first post

44 replies

Forum|alt.badge.img+15
  • Jamf Heroes
  • October 23, 2021

Huge thanks for any more help you can provide : 😀


You can give this a look:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName $computerName scutil --set HostName $computerName scutil --set LocalHostName $computerName # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldcomputername to $computerName" exit 0

Forum|alt.badge.img+5
  • Author
  • Contributor
  • October 23, 2021

You can give this a look:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName $computerName scutil --set HostName $computerName scutil --set LocalHostName $computerName # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldcomputername to $computerName" exit 0

Thanks. Giving it a try now. I might try and tweak it to run without a rename and just have it run to name the computer right after pre-stage is complete based on XXXX-Model-FirstName but I'll see how this works on my next pre-stage test.

 

Appreciate the help.


Forum|alt.badge.img+15
  • Jamf Heroes
  • October 23, 2021

Thanks. Giving it a try now. I might try and tweak it to run without a rename and just have it run to name the computer right after pre-stage is complete based on XXXX-Model-FirstName but I'll see how this works on my next pre-stage test.

 

Appreciate the help.


Anytime! 👊


Forum|alt.badge.img+5
  • Author
  • Contributor
  • October 23, 2021

Currently getting this error when using a policy to run the script 😞

 

Script result: scutil: invalid option -- M
usage: scutil interactive access to the dynamic store.

 

Here's my current iteration. With the XXXX's in place of an internal abbreviation.

 

#!/bin/bash

# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`

# Get original name of computer
#oldComputerName=`scutil --get ComputerName`

# Generate new computer name
computerName=$XXXX-$modelName-$fname

# Set new computer name locally
scutil --set ComputerName $computerName

scutil --set HostName $computerName

scutil --set LocalHostName $computerName

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon


exit 0

 


Forum|alt.badge.img+15
  • Jamf Heroes
  • October 23, 2021

Currently getting this error when using a policy to run the script 😞

 

Script result: scutil: invalid option -- M
usage: scutil interactive access to the dynamic store.

 

Here's my current iteration. With the XXXX's in place of an internal abbreviation.

 

#!/bin/bash

# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`

# Get original name of computer
#oldComputerName=`scutil --get ComputerName`

# Generate new computer name
computerName=$XXXX-$modelName-$fname

# Set new computer name locally
scutil --set ComputerName $computerName

scutil --set HostName $computerName

scutil --set LocalHostName $computerName

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon


exit 0

 


Maybe throwing those variables in the scutil command in quotes:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName "$computerName" scutil --set HostName "$computerName" scutil --set LocalHostName "$computerName" # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldComputerName to $computerName" exit 0

Forum|alt.badge.img+5
  • Author
  • Contributor
  • October 25, 2021

Maybe throwing those variables in the scutil command in quotes:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName "$computerName" scutil --set HostName "$computerName" scutil --set LocalHostName "$computerName" # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldComputerName to $computerName" exit 0

Holy **** that worked perfectly! I can't say thanks enough. That just accomplished what I've been trying to find a way to do for the past few weeks in other ways. Thanks again!


Forum|alt.badge.img+15
  • Jamf Heroes
  • October 25, 2021

Holy **** that worked perfectly! I can't say thanks enough. That just accomplished what I've been trying to find a way to do for the past few weeks in other ways. Thanks again!


AWESOME! I'm glad that worked! Anytime!


Forum|alt.badge.img+5
  • Author
  • Contributor
  • October 25, 2021

One thought occurred to me. It's unlikely but any ideas to avoid duplicate names besides appending the serial #?  Which I assume would be this line modified:

computerName=$internalNamingScheme-$modelName-$fname

To:computerName=$internalNamingScheme-$modelName-$fname-$serialNumber"

Also if you don't mind does that modified line look good?


Forum|alt.badge.img+15
  • Jamf Heroes
  • October 25, 2021

One thought occurred to me. It's unlikely but any ideas to avoid duplicate names besides appending the serial #?  Which I assume would be this line modified:

computerName=$internalNamingScheme-$modelName-$fname

To:computerName=$internalNamingScheme-$modelName-$fname-$serialNumber"

Also if you don't mind does that modified line look good?


computerName=$internalNamingScheme-$modelName-$fname-$serial


Forum|alt.badge.img+5
  • Author
  • Contributor
  • October 25, 2021

computerName=$internalNamingScheme-$modelName-$fname-$serial


All set! That worked! Thanks again! 🙂


Forum|alt.badge.img+3
  • New Contributor
  • December 5, 2022

Maybe throwing those variables in the scutil command in quotes:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName "$computerName" scutil --set HostName "$computerName" scutil --set LocalHostName "$computerName" # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldComputerName to $computerName" exit 0

Thank you for this! This worked for me too but I have a quick question. For this part of the script ( internalNamingScheme=XXXX ) it puts XXXX-MacBookPro etc etc on my computer name. How would I make it be the username of the user who is using the Computer?

 

Just for clarification, I am on SSO (Azure) on the machines as well.

 


Forum|alt.badge.img+15
  • Jamf Heroes
  • December 5, 2022

Thank you for this! This worked for me too but I have a quick question. For this part of the script ( internalNamingScheme=XXXX ) it puts XXXX-MacBookPro etc etc on my computer name. How would I make it be the username of the user who is using the Computer?

 

Just for clarification, I am on SSO (Azure) on the machines as well.

 


Could you use: 

 

lastLoggedInUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)

 

To get the username of the last logged in user? You can then do something like 

 

computerName=$lastLoggedInUser-$modelName

 


Forum|alt.badge.img+3
  • New Contributor
  • December 5, 2022

Could you use: 

 

lastLoggedInUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)

 

To get the username of the last logged in user? You can then do something like 

 

computerName=$lastLoggedInUser-$modelName

 


So, I would change ( internalNamingScheme=XXXX ) I would put $lastloggedInUser? and then in # Generate new computer name. I would do what you pasted which is ( computerName=$lastLoggedInUser-$modelName )?

 

Sorry, I am not perfect in scripts.


Forum|alt.badge.img+3
  • New Contributor
  • December 5, 2022

So, I would change ( internalNamingScheme=XXXX ) I would put $lastloggedInUser? and then in # Generate new computer name. I would do what you pasted which is ( computerName=$lastLoggedInUser-$modelName )?

 

Sorry, I am not perfect in scripts.


This is what I currently have.

 

 

#!/bin/bash

# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`

# Get original name of computer
oldComputerName=`scutil --get ComputerName`

# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname-$serial

# Set new computer name locally
scutil --set ComputerName "$computerName"

scutil --set HostName "$computerName"

scutil --set LocalHostName "$computerName"

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon

echo "Computer name has been changed from $oldComputerName to $computerName"


exit 0


Forum|alt.badge.img+15
  • Jamf Heroes
  • December 5, 2022

This is what I currently have.

 

 

#!/bin/bash

# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`

# Get original name of computer
oldComputerName=`scutil --get ComputerName`

# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname-$serial

# Set new computer name locally
scutil --set ComputerName "$computerName"

scutil --set HostName "$computerName"

scutil --set LocalHostName "$computerName"

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon

echo "Computer name has been changed from $oldComputerName to $computerName"


exit 0


I guess I would need to know what your desired naming scheme is, for example if you want `username-MacBookPro` then you would do:

#!/bin/bash # Variables lastLoggedInUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName) modelName=$(system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ',') # Get original name of computer oldComputerName=$(scutil --get ComputerName) # Generate new computer name computerName=$lastLoggedInUser-$modelName # Set new computer name locally scutil --set ComputerName "$computerName" scutil --set HostName "$computerName" scutil --set LocalHostName "$computerName" # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldComputerName to $computerName" exit 0

Forum|alt.badge.img+3
  • New Contributor
  • December 5, 2022

This is what I currently have.

 

 

#!/bin/bash

# Variables
internalNamingScheme=XXXX
lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'`
modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','`
serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'`
serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"`

# Get original name of computer
oldComputerName=`scutil --get ComputerName`

# Generate new computer name
computerName=$internalNamingScheme-$modelName-$fname-$serial

# Set new computer name locally
scutil --set ComputerName "$computerName"

scutil --set HostName "$computerName"

scutil --set LocalHostName "$computerName"

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $computerName
/usr/local/bin/jamf recon

echo "Computer name has been changed from $oldComputerName to $computerName"


exit 0


Just want to update, it worked, so I changed ( computerName=$internalNamingScheme-$modelName-$fname-$serial ) to ( computerName=$lastLoggedInUser-$modelName-$fname-$serial )

 

And now it works.


Forum|alt.badge.img+5
  • Contributor
  • May 8, 2023

Is there a way to rename computer name base on csv file with MAC address? 

MAC:                                  Comp_Name

1c:57:dc:29:2d:1a              AirBook-0001

2c:57:dc:30:2d:1b              AirBook-0002

3c:57:dc:31:2d:3b              AirBook-0003

 


ssagola
Forum|alt.badge.img+11
  • Contributor
  • November 14, 2023

Maybe throwing those variables in the scutil command in quotes:

#!/bin/bash # Variables internalNamingScheme=XXXX lastLoggedInUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` fname=`dscl . -read /Users/$lastLoggedInUser RealName | grep ^\\ | awk '{print $1}'` modelName=`system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[0-9]*//g' | tr -d ','` serial1=`ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'` serial=`sed -e 's/^"//' -e 's/"$//' <<<"$serial1"` # Get original name of computer oldComputerName=`scutil --get ComputerName` # Generate new computer name computerName=$internalNamingScheme-$modelName-$fname # Set new computer name locally scutil --set ComputerName "$computerName" scutil --set HostName "$computerName" scutil --set LocalHostName "$computerName" # Set the computer name in Jamf to reflect what is set locally on the computer /usr/local/bin/jamf setComputerName -name $computerName /usr/local/bin/jamf recon echo "Computer name has been changed from $oldComputerName to $computerName" exit 0

Hey, love the script works great but how would I add the $JSSID in the name? So the name may look like 123-iMac-Jane (the first 123 is the $JSSID)


Bretterson
Forum|alt.badge.img+4
  • New Contributor
  • January 27, 2025

Hey, love the script works great but how would I add the $JSSID in the name? So the name may look like 123-iMac-Jane (the first 123 is the $JSSID)


You've probably figured it out by now, but getting it via configuration profile works nicely: https://community.jamf.com/t5/jamf-pro/get-jss-id-locally-via-script/m-p/293088/highlight/true#M260441