Posted on 03-04-2024 08:19 AM
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:
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
Solved! Go to Solution.
Posted on 03-21-2024 02:25 AM
Removing the if statement fixes this for me but still not entirely sure why and how it correlates with the error message.
Posted on 03-04-2024 11:24 AM
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.
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.
Posted on 03-13-2024 03:54 AM
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:
I don't understand why it is suddenly incapable of finding the file when it reaches the If statement.
Posted on 03-21-2024 07:06 AM
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.
Posted on 03-21-2024 02:25 AM
Removing the if statement fixes this for me but still not entirely sure why and how it correlates with the error message.