Skip to main content
Solved

Set computer name User folder and Serial Number


Forum|alt.badge.img+5

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"

Best answer by GrahamJ

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 original
Did this topic help you find an answer to your question?

6 replies

DBrowning
Forum|alt.badge.img+24
  • Esteemed Contributor
  • 668 replies
  • April 5, 2022

@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/ 


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 7 replies
  • April 6, 2022
DBrowning wrote:

@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/ 


neat miss on my part


Forum|alt.badge.img+19
  • Honored Contributor
  • 582 replies
  • April 6, 2022

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

 

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

Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 7 replies
  • April 6, 2022
Tribruin wrote:

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

 

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

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"


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 7 replies
  • Answer
  • April 6, 2022

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


Forum|alt.badge.img+21
  • Valued Contributor
  • 321 replies
  • April 6, 2022

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings