Set computer name User folder and Serial Number

GrahamJ
New Contributor II

So the below script was working fine and now no longer wants too. What am i missing?

 

#!/bin/bash

CurrentUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
SerialNumber=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk -F':' '{print $2}' | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')

### Set the computer name using the jamf binary

/usr/local/bin/jamf setComputerName -target / -name "$CurrentUser"-"$SerialNumber"

1 ACCEPTED SOLUTION

GrahamJ
New Contributor II

okay, think i got it working.

 

#!/bin/bash

userName=$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)
SerialNumber=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk -F':' '{print $2}' | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')

computerName="$userName-$SerialNumber"

# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"

exit 0

View solution in original post

6 REPLIES 6

DBrowning
Valued Contributor II

@GrahamJ I'm going to guess you are on macOS 12.3.  python was removed in macOS 12.3.  https://www.macrumors.com/2022/01/28/apple-removing-python-2-in-macos-12-3/ 

GrahamJ
New Contributor II

neat miss on my part

Tribruin
Valued Contributor II

Recommend changing your script to use this command to get the Current User:

 

currentUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

GrahamJ
New Contributor II

Still missing something. Still kicking an -name error. 

#!/bin/bash

currentUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
SerialNumber=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk -F':' '{print $2}' | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')

### Set the computer name using the jamf binary

/usr/local/bin/jamf setComputerName -target / -name "$CurrentUser"-"$SerialNumber"

GrahamJ
New Contributor II

okay, think i got it working.

 

#!/bin/bash

userName=$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)
SerialNumber=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk -F':' '{print $2}' | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')

computerName="$userName-$SerialNumber"

# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$computerName"
/usr/sbin/scutil --set HostName "$computerName"
/usr/sbin/scutil --set LocalHostName "$computerName"

exit 0

jcarr
Release Candidate Programs Tester

Just remember that device names are broadcast in the clear on any associated network, so it is generally advisable to avoid PII in the device name if at all possible.  If all of your users are adults, it may not be an issue, but would be a HUGE red flag in K12 due to the privacy and safety concerns.  Just my $0.02.