Computer rename script

rasen66
New Contributor

Hi all.

Anyone have a quick shell script to rename computers by looking up the serial in a CSV file? Figure it should be easy to install a CSV on the computer and then run a script to rename?

Of is there another easier way to do this? Why? I duplicated a renaming policy and then didn't remove "all computers" from scope, causing buggered computer names. It would be nice to not have to fix manually.

5 REPLIES 5

Bol
Contributor III

@rasen66 I might not be understanding correctly but if all you want to do it rename them to serial numbers, you can add a "Files and Processes" payload to a policy, include the following;

 

jamf setComputerName -useSerialNumber

 

stevewood
Honored Contributor II
Honored Contributor II

Probably the easiest way I can think of is to use The MUT and an inventory policy. Use the Computer template CSV file from MUT, enter the serial numbers and the name you want each computer to be, upload to Jamf Pro using MUT, and then create a policy set to Once Per Computer that updates inventory and check the box to reset computer name to the record (on the maintenance tab). When a computer runs that policy the computer name will reset to what is in Jamf Pro. 

could also look at using inventory preload

stevewood
Honored Contributor II
Honored Contributor II

Also, you can use a similar method to rename individual computers when they get out of whack. Either use a Static Group or a Smart Group that looks at a field you don’t use like maybe one of the barcode fields (put text in that field to flag a machine for rename for example). Scope an inventory policy like I described above to that group, and then whenever you need to rename a device you go to the computer record, edit the name and edit the barcode field (since they’re both on the general tab), flush the policy from the history (if it’s ever been run on this device), and the computer should rename. Yes, you would need to cleanup the Static Group or the barcode field later, but you have an easy mechanism for renaming ad hoc. 

jcarr
Valued Contributor

It should be noted that it is generally NOT a good idea to use personally identifiable information (PII) in a device name. Since the device name is broadcast in the clear on any associated network, this could pose a potential security and safety risk.  For adult users in enterprise, this risk may not be a huge concern, but this should NEVER be done for K12 students. Just my $0.02.

 

As for the practical matter, if you have a device on your local network (e.g. the Mac mini you use for content caching) where you can host a .csv file available via HTTP, you can use this script:

 

 

#!/bin/sh

# Populates ARD info fileds 1-4 for devices as specified in data file located at URL in parameter $4

#Define local location for data file
localFile="/usr/local/data.csv" #Hidden location, change as necessary

#Identify device by serial number
SERIAL=`system_profiler SPHardwareDataType | awk '/Serial/ {print $NF}'`

#Download data file from web server (as defined by parameter 4) to local storage
curl -o ${localFile} $4
sleep 5

#Populate variables from data file
ARD1=$(cat $localFile | grep $SERIAL | awk -F',' ' { print $2 } ')
ARD2=$(cat $localFile | grep $SERIAL | awk -F',' ' { print $3 } ')
ARD3=$(cat $localFile | grep $SERIAL | awk -F',' ' { print $4 } ')
ARD4=$(cat $localFile | grep $SERIAL | awk -F',' ' { print $5 } ')

#Set ARD fields using jamf binary
/usr/local/bin/jamf setARDFields -target / -1 "$ARD1" -2 "$ARD2" -3 "$ARD3" -4 "$ARD4"

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

rm -rP $localFile

exit 0

 

 

Host the data file locally and pass the URL to the script using the appropriate parameter field in the policy. When run, the script parses the downloaded data file and sets the custom info fields for Apple Remote Desktop (PII can be used here as this info is encrypted in transport).  The computer name is set to a unique value using the en0 MAC address (WiFi MAC for devices with no on-board Ethernet), but could easily be set to a value in column 6 of the data file.

whiteb
Contributor

Our naming convention starts with lt or dt if Laptop or Desktop, then serial number, then macOS version at the end. It can be nice to be able to tell those things at a glance, or identify what they are when looking in DHCP or DNS for example.

Rename script:

#!/bin/bash

modelname=$(system_profiler SPHardwareDataType | grep "Model Identifier" | cut -d: -f2)

computertype="dt"

case "$modelname" in
*Book*)
    computertype="lt"
;;
*mini*)
    computertype="dt"
;;
*iMac*)
    computertype="dt"
;;
*MacPro*)
    computertype="dt"
;;
esac

serial=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`;
version=$(sw_vers | grep "ProductVersion" | awk '{ print $2; }')
os="m${version//./}"

echo
echo "This is the computer type: $computertype";
echo "This is the serial: $serial";
echo "This is the OS: $os";

computername=${computertype}${serial}${os};

echo "Using name $computername"
scutil --set HostName "$computername"
scutil --set LocalHostName "$computername"
scutil --set ComputerName "$computername"
networksetup -setcomputername "$computername"
dscacheutil -flushcache