Naming Convention Assistance

BWonderchild
New Contributor III

Hey everyone!

 

I'm trying to build out a script that would rename devices based on a local user account, and the serial number. For instance if their serial number was 123456 and their name was John Doe then the device would be "JDOE-123456"... Don't suppose anyone has already built this out?

3 REPLIES 3

junjishimazaki
Valued Contributor

How are you doing to deploy the script? I have nothing against helping but you should learn to try scripting first before asking the community for assistance. Otherwise, you won't really learn anything. 

Anyways..... 

I set up a policy to run the script at reboot after the account is created. I modified my original script to match what you are requesting:

#!/bin/sh

#Get current Mac serial number

serial_Number=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`

# checked currently logged in user and assign to variable
curUser="`stat -f%Su /dev/console`"
echo $curUser"-mbp"

# Rename computer and host name
scutil --set ComputerName $curUser"-$serial_Number"
scutil --set LocalHostName $curUser"-$serial_Number"
scutil --set HostName $curUser"-$serial_Number"

#Recon computer name and user to Jamf PRo
/usr/local/bin/jamf recon

exit 0

pbenware1
Release Candidate Programs Tester

Although for security reasons we stay away from using any sort of personal identifier in computer names, we do have a shell script that uses AppleScript OSAScript and dialogs to collect information about the name, sets it in the OS using scutil and submits to jamf via a recon.  We have it incorporated into our setup and provisioning script that we use for new computers.  The name piece is below.  We have other dialogs asking for user ID and a selectable list of optional apps to install; those are used elsewhere in the script.

When the script runs it pops up a dialog box with an input field for a Dept Code, and a second prompt for a unique identifier, then we build the name with some additional fixed characters (school (the "M" which is our schools designation, Laptop/desktop ("$comptype"), separators, etc) to the name.

Once the name is built the script pops a 3rd dialog asking the tech to confirm and modify as needed.  When finished they click Rename, and the name gets set in the OS and in Jamf, at the very end of the provisioning script.

Like everything there are a million ways to solve problems like this.  I'm sure this one has it faults, but it works for us.

# Collect Department ID Code for use in the computer name.
deptidentry=`osascript -e 'set T to text returned of (display dialog "Enter the Department Code:" buttons {"OK"} default button "OK" default answer "")'`

# Collect a Unique Identifier such as Asset ID, Serial, barcode number, etc.
assetlabelentry=`osascript -e 'set T to text returned of (display dialog "Enter an Asset ID (STAT Asset #, Serial etc):" buttons {"OK"} default button "OK" default answer "")'`

# Build the computer name
computername="M$(echo "$deptidentry" | tr '[:lower:]' '[:upper:]')"-$comptype-"$(echo "$assetlabelentry" | tr '[:lower:]' '[:upper:]')"
echo_log "The suggested computer name is $computername."

# Ask the tech to verify the computer name. Tech has a chance to verify or modify as needed. Save the result to variable $computername
computername=`osascript -e 'set T to text returned of (display dialog "Please make any necessary edits, then select Rename to set the computer name." buttons {"Rename"} default button "Rename" default answer "'$computername'")'`
 

 

AJPinto
Honored Contributor III

We match our Hostnames to the Device serial number. When dealing with a device itself the SN is far more important than the user its assigned to. You can find the user out with a few clicks anyway. Also putting user information in the hostname can reveal personally identifiable information as other devices on a network (starbucks for example) can see a hostname and in turn whatever user information you put in that hostname.