Script to Change First Two Letters of Device Name

GabeShack
Valued Contributor II

Hi All,
I'm looking to create a script that I can run to change just the first two letters of a Devices name. We currently use Building-student(or teacher) and barcode (all under 15 characters for our AD bind). Since moving to one-to-one this year, I need to move the 8th grade class to the high school. And since we renamed our Middle School I'd like to update the names on their devices as well.

I used to run a script to change the last 5 digits of a device name to be the asset tag number, but have not used that in awhile and can no longer find it lol. Im sure I can grab the existing name, but not sure about modifying it to what I want.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor II

You can get a subset of characters from a variable easily in the shell. The below works in /bin/bash and /bin/zsh and probably others as well, but I haven't tested beyond those.

% name="ABCDE1234567890"
% echo "${name:0:2}"
% AB

Using a similar process in the shell, you can also get the last x characters of a given string, which means you can add those to a new "prefix" string to come up with a new computer name. The syntax looks like this. This grabs the last 10 characters of the same example string:

% name="ABCDE1234567890"
% echo "${name: -10}"
% 1234567890

So...

#!/bin/zsh

## Get the existing computer name
computer_name=$(scutil --get ComputerName)
echo "Current computer name is $computer_name"

## Get the number of characters in the computer name
str_len="${#computer_name}"
echo "Computer Name length is: $str_len"

## Create a new string length, minus 2
len_minus_two=$((str_len-2))

## Get the last x number of characters from the computer name. This is our "suffix"
comp_name_end="${computer_name: -$len_minus_two}"
echo "Last $len_minus_two characters of the computer name are: $comp_name_end"

## Set the new characters for the start of the computer name here
new_prefix="XY"

## Set new computer name, combining new prefix with our suffix
new_comp_name="${new_prefix}${comp_name_end}"
echo "New computer name will be: $new_comp_name"

## Set the new computer name
scutil --set ComputerName "$new_comp_name"
scutil --set LocalHostName "$new_comp_name"

View solution in original post

3 REPLIES 3

mm2270
Legendary Contributor II

You can get a subset of characters from a variable easily in the shell. The below works in /bin/bash and /bin/zsh and probably others as well, but I haven't tested beyond those.

% name="ABCDE1234567890"
% echo "${name:0:2}"
% AB

Using a similar process in the shell, you can also get the last x characters of a given string, which means you can add those to a new "prefix" string to come up with a new computer name. The syntax looks like this. This grabs the last 10 characters of the same example string:

% name="ABCDE1234567890"
% echo "${name: -10}"
% 1234567890

So...

#!/bin/zsh

## Get the existing computer name
computer_name=$(scutil --get ComputerName)
echo "Current computer name is $computer_name"

## Get the number of characters in the computer name
str_len="${#computer_name}"
echo "Computer Name length is: $str_len"

## Create a new string length, minus 2
len_minus_two=$((str_len-2))

## Get the last x number of characters from the computer name. This is our "suffix"
comp_name_end="${computer_name: -$len_minus_two}"
echo "Last $len_minus_two characters of the computer name are: $comp_name_end"

## Set the new characters for the start of the computer name here
new_prefix="XY"

## Set new computer name, combining new prefix with our suffix
new_comp_name="${new_prefix}${comp_name_end}"
echo "New computer name will be: $new_comp_name"

## Set the new computer name
scutil --set ComputerName "$new_comp_name"
scutil --set LocalHostName "$new_comp_name"

View solution in original post

GabeShack
Valued Contributor II

@mm2270 This worked great thanks.  

 

Gabe Shackney
Princeton Public Schools

GabeShack
Valued Contributor II

@mm2270 I am now trying to remove our dependency on using DEP Notify which I only do to get our naming scheme, and am thinking about a slightly more complex script for naming.  I'm thinking I can pull everything I want in a name lookup from our Active Directory (which is tied into JAMF with our JIM).  So again our normal scheme is building-teacher/student,Barcode or HS-Student12345 or HS-Teacher12345 (and in many less cases HS-Admin12345).  Each has access to different policies.

I have all the barcodes already set with an inventory preload for the JSS, and the buildings are all tied to the students/staff members record in Jamf and Active Directory.  So I'm wondering if there is a way I can leverage this info by scripting a lookup either to JamfPro or to AD to grab these pieces once the user logs in?

For example, if the location is listed as Princeton High School, have it start the name with HS-, then determine if it is a student or staff member with a group lookup or I can have the JSS sync that field from AD if needed, and then lastly grab the 5 digit barcode assigned to the record of the computer since I have that already in a field.  

Any help you can provide would be amazing since you alway nail it.

Gabe Shackney
Princeton Public Schools