Change computer name to username-serialnumber

Philein
New Contributor III

Hi everyone

Can someone suggest me a good script to change my mac's name to "username-serialnumber" ?

Thanks

2 ACCEPTED SOLUTIONS

karthikeyan_mac
Valued Contributor

@Philein You can scutil or jamf binary

 

#!/bin/bash

serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
loggedinuser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

name=$loggedinuser-$serialNumber

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

 

or you can also use Jamf binary instead of scutil.

 

jamf setComputerName -name $name

 

 Thanks

View solution in original post

I do this...

### Get serial number
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial\ Number\ \(system\)/ {print $NF}'`

/usr/sbin/scutil --set ComputerName "${serial}"
/usr/sbin/scutil --set LocalHostName "${serial}"
/usr/sbin/scutil --set HostName "${serial}"

View solution in original post

8 REPLIES 8

karthikeyan_mac
Valued Contributor

@Philein You can scutil or jamf binary

 

#!/bin/bash

serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
loggedinuser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

name=$loggedinuser-$serialNumber

scutil --set HostName $name
scutil --set LocalHostName $name
scutil --set ComputerName $name

 

or you can also use Jamf binary instead of scutil.

 

jamf setComputerName -name $name

 

 Thanks

Thanks. It works but it returns me this:

Script exit code: 0
Script result: SCPreferencesSetLocalHostName() failed: Invalid argument

I do this...

### Get serial number
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial\ Number\ \(system\)/ {print $NF}'`

/usr/sbin/scutil --set ComputerName "${serial}"
/usr/sbin/scutil --set LocalHostName "${serial}"
/usr/sbin/scutil --set HostName "${serial}"

@Philein LocalHostName cannot have space dots. If your username has dot you may have to remove it.

loggedinuser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' | sed -e 's/\.//g' )

 You can use the change the "loggedinuser"  to strip the dots from the username. 

Thanks

markdmatthews
Contributor

you're also going to need to add:
/usr/local/jamf/bin/jamf recon

or a Maintenance to the policy to update Jamf

jcarr
Release Candidate Programs Tester

This is a good time to point out that PII in a device name is generally not a good idea.  Device names are broadcast in the clear on public networks, and personally identifiable information could be a security and/or safety risk.  If your users are adults, you're probably ok, but I would definitely NOT recommend this for K12 devices.

 

Just my $0.0.2

Philein
New Contributor III

Thanks, this is a valid point. 

AtillaTheC
Contributor II

 

We have periods in the usernames, I use this script

#!/bin/sh

serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
currUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' | sed -e 's/\.//g' )
name=$currUser-$serial
/usr/sbin/scutil --set ComputerName $name
/usr/sbin/scutil --set HostName $name
/usr/sbin/scutil --set LocalHostName $name

exit 0