Rename computer based on Barcode 1 Value

mgoodall
New Contributor II

Good Morning,

We have been using this script for quite sometime now without issue and it has been working for our intended purpose.

However, we have a few machines now on BigSur that are not completing and are setting the name to the devices serial number rather than what is in Barcode 1

The script we are using is here

#!/bin/bash

SERIAL_NUMBER=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
JSS=https://tasis.tramscloud.co.uk
API_USER="$4"
API_PASS="$5"
BARCODE_1_INFO=$(curl -k $JSS/JSSResource/computers/serialnumber/"$SERIAL_NUMBER" --user "$API_USER:$API_PASS" | xpath /computer/general/barcode_1 | awk '{gsub(/<[^>]*>/,"")};1')
SUFFIX=""

if [ -n "$BARCODE_1_INFO" ]; then
  echo "Processing new name for this client..."
  echo "Changing name..."
  jamf -setComputerName -name "$BARCODE_1_INFO""$SUFFIX""
  echo "Name change complete "$BARCODE_1_INFO""$SUFFIX""

else
  echo "Bar code information was unavailable. Using Serial Number instead."
  echo "Changing Name..."
  jamf -setComputerName -name ""$SERIAL_NUMBER""$SUFFIX""
  echo "Name Change complete "$SERIAL_NUMBER""$SUFFIX"
fi

When it runs against a machine on BigSur, these are the results that we get.

Script result:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0Usage:
/usr/bin/xpath5.28 [options] -e query [-e query...] [filename...]

If no filenames are given, supply XML on STDIN. You must provide at
least one query. Each supplementary query is done in order, the
previous query giving the context of the next one.

Options:

-q quiet, only output the resulting PATH.
-s suffix, use suffix instead of linefeed.
-p postfix, use prefix instead of nothing.
-n Don't use an external DTD.

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 24103    0 24103    0     0  49089      0 --:--:-- --:--:-- --:--:-- 48989
curl: (23) Failed writing body (0 != 8184)
Bar code information was unavailable. Using Serial Number instead.
Changing Name...
Set Computer Name to SERIAL NUMBER REMOVED
Name Change Complete (SERIAL NUMBER REMOVED)

I am guessing that its an issue within the BARCODE_1_INFO variable but im not sure how to edit it correctly to fix the issue.

BARCODE_1_INFO=$(curl -k $JSS/JSSResource/computers/serialnumber/"$SERIAL_NUMBER" --user "$API_USER:$API_PASS" | xpath /computer/general/barcode_1 | awk '{gsub(/<[^>]*>/,"")};1')

I should point out that the device does have something within the Barcode 1 field within JAMF.

As always, help gratefully received!

2 REPLIES 2

Mauricio
Contributor III

@mgoodall One of the changes:
Big Sur xpath changes

brockwalters
Contributor II

you should switch to using /usr/bin/xmllint -xpath in all scripts....