Skip to main content
Question

I am working on a script to change computer name to LPT + Serial Number.


Forum|alt.badge.img+1

My Script doesnt seem to be working any ideas. Can anyone see what is wrong.

#!/bin/sh

#get serial number
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

#Define Laptop
laptop=$LPT

computerName="$LPT-$serial"

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

 

 

7 replies

Forum|alt.badge.img+9
  • Valued Contributor
  • 109 replies
  • February 24, 2023

Well, a couple of things here. First you are creating the variable laptop and giving it the value of the variable $LPT which has no value because it has not been declared anywhere in the script.

Second, you don't need to declare the value $laptop or $LPT to pass a string to another variable.

So if you want to name your computers LPT-<serial> you could use:

 

 

#!/bin/sh #get serial number serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') #Define Laptop laptop="LTP" computerName="$laptop-$serial" # Set the ComputerName, HostName and LocalHostName /usr/sbin/scutil --set ComputerName "$computerName" /usr/sbin/scutil --set HostName "$computerName" /usr/sbin/scutil --set LocalHostName "$computerName"

 

 

 or you could simply do:

 

 

#!/bin/sh #get serial number serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') #Define Laptop computerName="LPT-$serial" # Set the ComputerName, HostName and LocalHostName /usr/sbin/scutil --set ComputerName "$computerName" /usr/sbin/scutil --set HostName "$computerName" /usr/sbin/scutil --set LocalHostName "$computerName"

 

 

 


Forum|alt.badge.img+21
  • Valued Contributor
  • 322 replies
  • February 24, 2023

If you are using the convention "[Prefix]-[SERIALNUMBER]" this can be done with a single line:

 

/usr/local/bin/jamf setComputerName -name "Mac-$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')"

 

 

...or if you prefer to use the Wi-Fi MAC address:

 

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

 

 

This allows you to use the 'Files and Processes' payload rather than creating and maintaining a script and then adding the script to a policy.


mvu
Forum|alt.badge.img+20
  • Jamf Heroes
  • 909 replies
  • February 24, 2023

Using something like this:

 

#!/bin/bash

OLDCOMPNAME=`/usr/sbin/scutil --get ComputerName`
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -computerinfo -set1 -1 "${OLDCOMPNAME}"
PREFIX="LPT"
SERIAL=`system_profiler SPHardwareDataType | grep "r (system)" | awk '{ print $4}'`
HOSTNAME="${PREFIX}${SERIAL}"
sudo /usr/sbin/scutil --set ComputerName "${HOSTNAME}"
sudo /usr/sbin/scutil --set LocalHostName "${HOSTNAME}"

exit 0


Forum|alt.badge.img+6
  • Contributor
  • 99 replies
  • February 24, 2023

Might not be a bad idea to detect if it's a laptop instead of hard coding LPT into the script. Searching for the presence of a battery should do that. Or you could set LPT as a parameter and feed it to the script from the policy, this way should you need the script for both laptops and desktops, you could have two policies call the same script.  As for detecting battery, this should do it (I suggest testing this!)

#!/bin/zsh serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') deviceType=$(ioreg -l -w0 | grep capacity) if [ -z "$deviceType" ]; then pre="DT" else pre="LPT" fi computerName="$pre-$serial" echo $computerName

I believe using "book" from model ID these days isn't as reliable as it once was, could be wrong though.


YanW
Forum|alt.badge.img+11
  • Contributor
  • 180 replies
  • February 24, 2023

You can change the prefix however you like

#!/bin/bash serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') modelID=$(sysctl hw.model | awk '{print $NF}') case "$modelID" in iMac*) modelPrefix="iMac" ;; MacBook*) modelPrefix="MBA" ;; MacPro*) modelPrefix="MBP" ;; Macmini*) modelPrefix="Mini" ;; *) modelPrefix="Mac" ;; esac name=$modelPrefix-$serialNumber jamf setComputerName -name $name

 


pete_c
Forum|alt.badge.img+16
  • Honored Contributor
  • 252 replies
  • April 25, 2023
YanW wrote:

You can change the prefix however you like

#!/bin/bash serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') modelID=$(sysctl hw.model | awk '{print $NF}') case "$modelID" in iMac*) modelPrefix="iMac" ;; MacBook*) modelPrefix="MBA" ;; MacPro*) modelPrefix="MBP" ;; Macmini*) modelPrefix="Mini" ;; *) modelPrefix="Mac" ;; esac name=$modelPrefix-$serialNumber jamf setComputerName -name $name

 


system_profiler is pretty slow, relatively speaking. Try replacing it with ioreg:

serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\\" '/IOPlatformSerialNumber/{print $(NF-1)})

Forum|alt.badge.img+6
  • Contributor
  • 99 replies
  • April 25, 2023
ubcoit wrote:

Might not be a bad idea to detect if it's a laptop instead of hard coding LPT into the script. Searching for the presence of a battery should do that. Or you could set LPT as a parameter and feed it to the script from the policy, this way should you need the script for both laptops and desktops, you could have two policies call the same script.  As for detecting battery, this should do it (I suggest testing this!)

#!/bin/zsh serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') deviceType=$(ioreg -l -w0 | grep capacity) if [ -z "$deviceType" ]; then pre="DT" else pre="LPT" fi computerName="$pre-$serial" echo $computerName

I believe using "book" from model ID these days isn't as reliable as it once was, could be wrong though.


Another way to detect for Desktop vs laptop.

battery=$(pmset -g batt | grep -Eo "\\d+%" | cut -d% -f1)


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