/Library/Application/ Support/JAMF/tmp... File Not Found When Running Script

TitusS
New Contributor II

We are trying to run a script that will change a Mac's name to the asset tag of the device. It is supposed to run on devices at an ongoing check in on each device but it won't make it past the first test run. I know the script is fine running locally as expected but running the policy on JAMF (via a scope and self-service) gives me this error:

Screenshot 2024-03-04 at 15.30.36.png

Shell type is set as "Shell/Bash" and here is the code (omitted username, password and url for security reasons but tested each individually and they work as expected). 

 

#!/bin/zsh

# server and credential information
jamfProURL=url
username="username"
password="password" 

# request auth token
authToken=$( /usr/bin/curl \
--request POST \
--silent \
--url "$jamfProURL/api/v1/auth/token" \
--user "$username:$password" )

echo "$authToken"

# parse auth token
token=$( /usr/bin/plutil \
-extract token raw - <<< "$authToken" )

tokenExpiration=$( /usr/bin/plutil \
-extract expires raw - <<< "$authToken" )

localTokenExpirationEpoch=$( TZ=GMT /bin/date -j \
-f "%Y-%m-%dT%T" "$tokenExpiration" \
+"%s" 2> /dev/null )

echo Token: "$token"
echo Expiration: "$tokenExpiration"
echo Expiration epoch: "$localTokenExpirationEpoch"

#get the serial number

serialNumber="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*\"\(.*\)\"/\1/')"

#get the asset tag from jamf

assetTag=$(/usr/bin/curl -H "Accept: text/xml" --header "Authorization: Bearer $token" "${jamfProURL}/JSSResource/computers/serialnumber/${serialNumber}/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<asset_tag>/{print $3}')

# set computer name 

if [ "$assetTag" == "" ]; then
 echo "Asset Tag is empty. Exiting..."
 exit 1
else 
 /usr/sbin/scutil --set HostName "$assetTag-mac"
 /usr/sbin/scutil --set LocalHostName "$assetTag-mac"
 /usr/sbin/scutil --set ComputerName "$assetTag-mac" 
fi

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "$assetTag"

/usr/local/jamf/bin/jamf recon

 

 I saw a few people finding out that the error was actually within the code used but since I don't have any experience with shell scripting I can't tell for sure if it's a JAMF issue or scirpting issue.

Any help would be appreciated. Thanks

1 ACCEPTED SOLUTION

TitusS
New Contributor II

Removing the if statement fixes this for me but still not entirely sure why and how it correlates with the error message.

View solution in original post

4 REPLIES 4

spesh
New Contributor III

It is a bit difficult to understand the error you are seeing based upon that screenshot, so take this with a grain of salt. A couple of things come to mind when looking over your script. 

  1. Make sure your password is being passed correctly. I would recommend using single quotes instead of double quotes as single quotes are considered raw strings. Meaning, special characters won't be escaped. 
  2. (Optional) Change your serialNumber variable declaration. I've personally never had an issue with serial=$(system_profiler SPHardwareDataType | awk '/Serial Number/{print $4}') obtaining the proper result. Its possible the API call is not successful because the serial number is not coming back as expected. 
  3. Only other thing that immediately stuck out to me was the way you are parsing the API response for the asset tag information. I might recommend using --xpath with xmllint instead of --format: | xmllint --xpath '/computer/general/asset_tag/text()' -)

Other than that, you can always use set -x to debug the script locally on a separate machine to see specifically where the script fails. 

TitusS
New Contributor II

I have implemented some of the recommended fixes for the script with trial and error, debugging within the script (set -x) and created a log file on devices. These are the final 3 lines of output:

Screenshot 2024-03-13 at 10.53.55.png

 I don't understand why it is suddenly incapable of finding the file when it reaches the If statement.

spesh
New Contributor III

Out of curiosity, what if you tried: 

if [[ -z "$assetTag" ]]; then
    # Asset tag is empty
    echo "Asset tag is empty. Exiting..."
    exit 1
else
    ...

The -z returns true if the length of a string is zero. Otherwise I'm honestly not sure why that if statement is failing.  

TitusS
New Contributor II

Removing the if statement fixes this for me but still not entirely sure why and how it correlates with the error message.