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