Auto-generated custom machine name at Enrollment?

jkf
New Contributor III

Hey folks,

Our machine naming convention is 11 characters long. The first 4 characters are:

m = mac

d/p = desktop/portable

21 = deployment year

...then the last 7 characters of that machine's serial number.

We can't change this naming convention operationally/culturally (eg use the full serial). It has to be the way it is.

Currently at enrollment, we display the full serial number on the screen, and the person imaging it will count 7 characters back, and manually type/paste that in. This lends itself to errors, that don't always get caught in time.

We can sort out automating the first 4 characters, but does anyone have a clever idea about how we could automatically put the last 7 of the serial into the name?

 

1 ACCEPTED SOLUTION

naschenbrenner
New Contributor III

Edit: sorry I wasn't quite as done as I thought and thought you said first 7 at first, this has been fixed to be last 7.

We do something similar, but use different digits. After a few minutes of  modifying ours this is the script I came up with to get you the last 7. You'll need to build  out the rest but this should get you started.

 

 

#!/bin/sh

### Get the last 7 serial numbers 
lastSerialChar=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 6-12)

 
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')



computerName=$lastSerialChar

echo $computerName

 

 

 

 

 

 

 

View solution in original post

5 REPLIES 5

naschenbrenner
New Contributor III

Edit: sorry I wasn't quite as done as I thought and thought you said first 7 at first, this has been fixed to be last 7.

We do something similar, but use different digits. After a few minutes of  modifying ours this is the script I came up with to get you the last 7. You'll need to build  out the rest but this should get you started.

 

 

#!/bin/sh

### Get the last 7 serial numbers 
lastSerialChar=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 6-12)

 
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')



computerName=$lastSerialChar

echo $computerName

 

 

 

 

 

 

 

jkf
New Contributor III

YES! This is exactly what I needed. Thanks @naschenbrenner !

Tribruin
Valued Contributor II

The only thing to be aware is that Apple is changing their serial numbers to a random 10 digit number. I think the M1 iMacs were the first and, very likely, the new MacBook Pros will use it as well. You can try this as well:

 

``` 

serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')

lastSerialChar=${serial: -7}

 

That should get the last 7 digits regardless of the length of Serial Number. 


(And that reminds me, I need to go look at my scripts!)

jkf
New Contributor III

Thanks @Tribruin ! Confirmed that your solution works for us, too.

We're aware of Apple's randomized serials. The actual content of the name just needs to be unique: our convention is a hold-over from years of naming Windows machines similarly (and so we're not parsing 800+ machines called "Lab iMac" or "<username>'s MacBook Pro"). As machines change users and departments, it remains consistent, and the first 4 characters are a Quick Hit for what you're working on, and when they're up for a refresh. There was talk of using full serials, but each vendor uses a different number of characters, and that has its own issues.

greatkemo
Contributor II

I have a similar naming scheme, and I use a script to automate it.

 

In my case as we are an educational institution, we follow this naming convention.

For user John Smith, Receiving his MacBook Pro in Fall 2021 would look something like this

FSM-JSMIXYZ-F21

FS = Faculty/Staff (we do not need to differentiate between the 2 job roles)

M = Mac, we do not need to differentiate between desktop and laptop for faculty and staff, they are managed the same.

JSMI = First initial, Last name first 3 letters.

XYZ = Last three characters of the serial number

F = Fall (Spring, S. Summer, M)

21 = 2021

 

In your case you could simply add another if statement that looks at the machine model using sysctl -n hw.model command, and if like book then equals portable, else desktop etc.

 

The script is run after enrollment as part of a DEPNotify workflow.

 

Hope this helps.

 

 

#!/bin/bash

#get computer serial number and shorten it
part_serial=$(ioreg -l | awk '/IOPlatformSerialNumber/ { print $NF;}'|tr -d '\"'| cut -c 10-12)

#get current user's full name and shorten it to 4 characters
part_name=$(dscl . -read "/Users/$(ls -l /dev/console | awk '{print $3}')" RealName | sed -n 's/^ //g;2p' | sed 's/[^a-z  A-Z]//g' | awk '{print toupper(substr($1,1,1)$NF)}' | cut -c 1-4)

#define prefix
name_prefix="FSM"

# get term and year if month 01-05 = Spring (S) 06-07= Summer (M) 08-12= Fall (F)
deploy_year=$(date "+%y")
deploy_month=$(date "+%m" | sed 's/^0*//')

if [[ "${deploy_month}" -ge "1"  ]] && [[ "${deploy_month}" -le "5"  ]]; then
  deploy_term="S"
elif [[ "${deploy_month}" -ge "6" ]] && [[ "${deploy_month}" -le "7"  ]]; then
  deploy_term="M"
elif [[ "${deploy_month}" -ge "8" ]] && [[ "${deploy_month}" -le "12"  ]]; then
  deploy_term="F"
fi

#define computer name using prefix, encoded name and suffix
computer_name="${name_prefix}-${part_name}${part_serial}-${deploy_term}${deploy_year}"
echo "${computer_name}"

# Set computer name
scutil --set LocalHostName "${computer_name}"
scutil --set HostName "${computer_name}"
scutil --set ComputerName "${computer_name}"
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist NetBIOSName "${computer_name}"

exit 0

 

 

Borrowing from @naschenbrenner above, yours should look more like this now, that is if I understood you requirements correctly. 

 

#!/bin/sh

prefix="m"
model=$(sysctl -n hw.model)
serialnumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}' | cut -c 6-12)
year=$(date "+%y")

if expr "$model" : "*book*" > /dev/null; then
  type="p"
else
  type="d"
fi

computername="${prefix}${type}${year}${serialnumber}"
echo "${computername}"

scutil --set LocalHostName "${computername}"
scutil --set HostName "${computername}"
scutil --set ComputerName "${computername}"
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist NetBIOSName "${computername}"

exit 0

 

But as usual test test test before using in production.

 

Best regards,

Kamal