Variable Parameters to Change Computer Name via script.

smithdr
New Contributor III

Hello all,

I'm new to scripting, trying to learn as I go. One project that I need to complete is to create a script that changes the computer name to a specified naming convention. the name would include serialnumber and the site that the computer is located at. I'm having a hell of a time trying to get Parameter 4 to pass through $SERIALNUMBER.

Can anyone provide me with an example of how I can implement the variable parameters into my script correctly? I couldn't find quite what I'm looking for in other discussions.

Thank you!

15 REPLIES 15

mm2270
Legendary Contributor III

Hi. I would not look at using a script parameter to pass the serial number to the script, since you can easily grab that information within the script when it runs and store it as a variable to use later when (re)naming the Mac.

Script parameters are intended to pass something that would apply to any machine its being run on. Since the Serial Number is obviously different on each Mac, that's not a good place to try to plug it into the script.

For example, within your computer naming script, you can easily capture the Mac serial number in a few different ways. The one I tend to use is below

SerNum=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

Later you would use the variable $SerNum as the stored serial number in the naming portion of the script. That would be up to you to figure out since I don't know what other naming convention stuff you need to include in the name.

mm2270
Legendary Contributor III
 

smithdr
New Contributor III

Thanks @mm2270 ! The amount of time it would have taken me to get to that point is astounding. I really appreciate it.

To give everyone more detail, my naming convention requires that I include site name abbreviation, model name abbreviation and the serial number. So, I'm hoping to pass through the site name and use an if/then to say that if site = Phoenix - print AZ in the computername. The idea is that the naming convention would be AZiMAC_SERIALNUMBER for example. I don't know if I'm over complicating it by throwing in sites and models as variables as well.

Thanks again for the serial number help!

dmw3
Contributor III

@smithdr just remember that if you are binding to AD the computer name is limited to 15 characters.

TJ_Edgerly
New Contributor III

@smithdr it may involve setting up multipl naming policies, but this is how we accomplish this.

*Note: This is run on a non bound mac. Like @dmw3 said....15 characters in the limit for a computer name binding to AD

#!/bin/sh

prefix="AZ_"

/usr/local/jamf/bin/jamf setComputerName -useSerialNumber -prefix "$prefix"

###Set New Computer Name

echo $ComputerName
scutil --set HostName "AZ_"$ComputerName
scutil --set LocalHostName "AZ_"$ComputerName
scutil --set ComputerName "AZ_"$ComputerName


##### recon to update JSS
jamf recon

You would need to run the script via a policy scoped to a smart group at each site. I only have 3 sites...so i find the smart group easier than trying to write the if/than statements.

(You could also swap the hard code prefix "AZ_" with a $prefix...but i tend to prefer the hard code so as i'm bouncing back and forth...i'm reminded which group i'm changing)

smithdr
New Contributor III

@TJ.Edgerly Thank you! As I was writing my last post I knew it was a cumbersome solution. I have 5 sites I'm working with right now so it would be less work for me to go with your solution then to write out all of the if/thens. I will likely just make a version of the script for MacBook Airs and iMacs and scope them appropriately.

mm2270
Legendary Contributor III

If you have more than a couple of possible Sites, you don't have to use a bunch of if/then statements. You can use a case statement, which, will still not short, does simplify things a bit.

The only real question I have is, how did you plan in the script to determine the Site the Mac is part of? Is that already part of the local Mac information somewhere, like in a hidden file maybe? If not, you would likely need to use the JSS API to grab that info from the JSS. But then we're starting to get into a semi-complicated script.

smithdr
New Contributor III

@TJ.Edgerly , The script is working correctly except for the last ComputerName command. I receive an "Invalid argument" error. I get the same error if I run that command independently. Any thoughts?

EDIT: Disregard this. I actually combined the two suggestions in this post and got the result I was looking for. My code looks like:

#!/bin/sh

prefix="AZMBA_"

SerNum=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

#Set New Computer Name

echo $SerNum
scutil --set HostName "AZMBA_"$SerNum
scutil --set ComputerName "AZMBA_"$SerNum
scutil --set LocalHostName "AZMBA-"$SerNum

#recon to update JSS

jamf recon

exit 0

TJ_Edgerly
New Contributor III

@smithdr Awesome!

@mm2270 in our environment, we set the site during enrollment (Pre-Stage Enrollment with DEP in our case) so we just scope the rename policy to run once with a smart group for the site. Also, we run something like:

/usr/local/jamf/bin/jamf recon -building "$4" -department $5

and use Variable 4 & 5 on the JSS Parameters to set Building and Department too.

dlondon
Valued Contributor

Hi Daniel,

1) Not sure if that was a generic example or not that you gave of the script. People said this earlier but I'll repeat it. The size allowed by AD of the computer name is 15. The serial number is 12 so you only have 3 characters left for your prefix (not the 6 you show)

2) I used to like names that described what the machine is but I'm over that now. My feeling is that is what the inventory info is for so if you want a report you have the field showing model

3) in your #Set New Computer Name section you could use

/usr/local/bin/jamf setComputerName -prefix "AZ-" -useSerialNumber

for more info on usage try this in the command line ... jamf help setComputerName

Regards,

David London

dmw3
Contributor III

@smithdr With our script we use the last 7 characters of the serial number which are unique (at least so far), we use this to select the last 7 numbers:

lastCharsInSerialNum=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '.{7}$')

smithdr
New Contributor III

Hi @dlondon ,

I agree that the naming method is a bit long. We currently don't use AD in this organization. I just joined this company a little over a month ago and Jamf was purchased a little over a week ago, so one thing my management wants is for us to have all of the computer names be consistent. The naming example I mentioned above is what the standard is currently set to here.

I'm possibly going to use @dmw3 's suggestion with using the last 7 characters of the SN in the future if we implement an AD infrastructure.

stevenjklein
Contributor II

@dlondon
Great tip about the jamf verb setComputerName. It let me change this:

ioreg -l | grep IOPlatformSerialNumber | awk '{ print $4 }' | sed s/"/ml-/ | sed s/"//

to this eminently readable replacement:

jamf setComputerName -prefix "ml-" -useSerialNumber

The purpose of that line is so obvious it doesn't need a comment.

Here's my new script:

#!/bin/sh

# setname by Steven Klein; tested 2017-08-02
#
# This script sets ComputerName for Macs.
# Laptops will be named ml-serialnumber;
# Desktops will be named md-serialnumber.
#
# Note: Does not change the HostName or LocalHostName


# Get the first word of the computer name.  Possible values include:
#         iMac: iMacs
#          Mac: Mac mini or Mac Pro
#      MacBook: MacBook, MacBook Air, or MacBook Pro
#
ModelType=$(system_profiler SPHardwareDataType | grep 'Model Name:' | awk '{ print $3 }')

# for MacBook's set the name to ml-serialnumber. For all other Macs, set name to md-serialnumber
#
if [ "$ModelType" = "MacBook" ]; then
    jamf setComputerName -prefix "ml-" -useSerialNumber
else
    jamf setComputerName -prefix "md-" -useSerialNumber
fi

exit 0

Thanks, David!

ryankim
New Contributor II

Hi,

Is there a way to name a MAC device by its Site and Room information?
Ideal format is Site##-Room##-SerialName. (ie. 01-105-ABCDEF123456)

We currently have a separate policy scoped to each 48 different site using below script.

!/bin/bash

serialNumber=system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'
concatName=$4$5$serialNumber

sudo scutil --set ComputerName $concatName
sudo scutil --set LocalHostName $concatName
sudo scutil --set HostName $concatName

sudo /usr/local/bin/jamf recon

Then we pass Site## in $4 and Room## in $5

I am hoping that we can replace all these policies with 1 policy scoped to ALL site where we can name the Mac devices with the case statement.

Something like:
case "$SiteName" in 'Alpha' ) $4='01' ;; 'Central' ) $4='02' ;; 'Mountain' ) $4='03' ;; "North" ) $4='04' ;; "South" ) $4='05' ;;
esac

Is this possible? Your help would be highly appreciated.

Thank you.

Some details:
We have a matching Building created in Jamf but we do NOT use Building attribute and if the scripting would be easier with Building rather than Site, I guess we can create one policy to add that information. (if $SiteName='Alpha', Populate $Building as 'Alpha')

jp2019
New Contributor III

Hi,
I would like to use this script below and incorporate what was provided above by rtrouton: ioreg -rd1 -c IOPlatformExpertDevice | awk -F'["|"]' '/model/{print $4}' | grep "Book".
Can anyone help with how make the script below with the suggestion above so that the script names the computer MAC-L${appendserial} if it is a macbook, if model does not contain "Book" then computer name is MAC-D${appendserial}. see my script below

!/bin/sh

set serial variable

serial=/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'
appendserial=echo "${serial:5}"
name=MAC-D${appendserial}
/usr/sbin/scutil --set ComputerName "${name}"
/usr/sbin/scutil --set LocalHostName "${name}"
/usr/sbin/scutil --set HostName "${name}"
exit 0

Thank you