Posted on 07-10-2024 06:58 AM
We had a script that renamed the MACS (Building Name:Serial Number). Very simple but it no longer works. Jamf Support hasn't been able to help me and I am stuck. It has worked for years, and now has stopped working. Thoughts, help?
#!/bin/sh
# Name: rename_from_building_api.sh
# Description: This script renames a computer
# Created: 2017-10-03
# Last Modified: 2018-06-11
# Created by: SWeinstein
###
### Wait for dock to load
dockLoaded=$(pgrep -x Dock)
echo "Waiting for user to login..."
while [[ "$dockLoaded" == "" ]]
do
echo "Waiting for dock to load."
sleep 10
dockStatus=$(pgrep -x Dock)
done
sleep 5
loggedinuser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
echo "$loggedinuser is logged on!"
##### Set computer name from user name
loggedInUser=$(python3 -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
echo $loggedInUser
# Get logged in user's shortname by checking console user since the current logged in user owns the console.
userName=`/usr/bin/who | awk '/console/{ print $1 }'`
jssUser='apitest'
jssPass='api123'
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -k -H "accept: application/xml" -u ****:**** https://****:8443/JSSResource/computers/serialnumber/${serial}/subset/location)
BUILDING=$(echo $response | /usr/bin/awk -F'<building>|</building>' '{print $2}');
USERNAME=$(echo $response | /usr/bin/awk -F'<username>|</username>' '{print $2}');
echo $building
lasdName=''$building''$serial
# Setting computername
echo "Setting computer name..."
scutil --set ComputerName "$lasdName"
scutil --set HostName "$lasdName"
scutil --set LocalHostName "$lasdName"
exit 0
Posted on 07-10-2024 07:16 AM
#!/bin/sh
serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}' )
if [[ ${serialNumber} != "" ]]; then
echo "Serial Number is $serialNumber, setting ComputerName/LocalHostName/Hostname."
/usr/sbin/scutil --set ComputerName ${serialNumber}
/usr/sbin/scutil --set LocalHostName ${serialNumber}
/usr/sbin/scutil --set HostName ${serialNumber}
else
echo "Serial Number is blank"
fi
exit 0
Posted on 07-10-2024 07:25 AM
You're using basic authentication in your script (Username / Password). Basic Authentication was deprecated by Jamf on 31st March 2024.
So you might want to have a look at Bearer Token Authentication here:
https://community.jamf.com/t5/tech-thoughts/how-to-convert-classic-api-scripts-to-use-bearer-token/b...
https://developer.jamf.com/jamf-pro/recipes/bearer-token-authorization