Skip to content

Instantly share code, notes, and snippets.

@shurkin18
Created September 10, 2021 18:07
Show Gist options
  • Save shurkin18/4e14cfb929b48e3944e38d91486a7c0e to your computer and use it in GitHub Desktop.
Save shurkin18/4e14cfb929b48e3944e38d91486a7c0e to your computer and use it in GitHub Desktop.
JAMF UAPI script which will ask the user for computer name and update it on JSS and locally
#!/bin/bash
#################################################################################################################
# Update the following variables as needed: "apiUser", "apiPass", "apiURL"
#################################################################################################################
# server connection information
jssurl="API URL"
username="API USERNAME"
password="API PASSWORD"
################################################################
# DO NOT TOUCH BELOW UNLESS YOU KNOW WHAT YOU ARE DOING! #######
################################################################
# created base64-encoded credentials
encodedCredentials=$( printf "$username:$password" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
# generate an auth token
authToken=$( /usr/bin/curl "$jssurl/uapi/auth/tokens" \
--silent \
--request POST \
--header "Authorization: Basic $encodedCredentials" )
# parse authToken for token, omit expiration
token=$( /usr/bin/awk -F \" '{ print $4 }' <<< "$authToken" | /usr/bin/xargs )
#///////////////////////
#Start the UAPI scripts#
#prompt user for machine name
computernameinput=$(/usr/bin/osascript << EOF
display dialog "Enter the name of the current mac" default answer "" buttons {"OK"} default button 1
text returned of the result
EOF)
if [ "$computernameinput" != "" ]; then
echo "Computer name entered is: $computernameinput"
#Update/change local computer/host/localhostnames per user input
/usr/sbin/scutil --set HostName $computernameinput
/usr/sbin/scutil --set LocalHostName $computernameinput
/usr/sbin/scutil --set ComputerName $computernameinput
#pass the variable to JSS via UAPI as computer name
#determine current machine's UDID
udid=$(/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Hardware UUID:/ { print $3 }')
#Determine JSS ID of the machine using new UAPI via correct syntax
idraw=`curl --request GET \
--url "$jssurl/uapi/v1/computers-inventory?section=GENERAL&page=0&page-size=100&sort=id%3Aasc&filter=udid%3D%3D$udid" \
--header "Accept: application/json" \
--header "Authorization: Bearer $token" | sed -n '4 p'`
#echo "idraw is $idraw"
#Extract the computer ID from raw data from JSS UAPI
#echo "idraw is: $idraw"
id="$(echo -e "$idraw" | cut -d '"' -f4)"
echo "id is: $id"
#Change/update machine name on JSS per user's input via UAPI
curl --request PATCH \
--url "$jssurl/uapi/v1/computers-inventory-detail/$id" \
--header "Accept: application/json" \
--header "Authorization: Bearer $token" \
--header "Content-Type: application/json" \
--data '
{
"general":
{
"name" : "'$computernameinput'"
}
}
'
#Tell user the name of the machine they typed in and that it will be updated on JSS
runCommand=$( /usr/bin/sw_vers -productVersion)
## Format information #####
displayInfo="The computer name you typed in is: $computernameinput, it will now be updated on JSS."
runCommand="button returned of (display dialog \"$displayInfo\" with title \"Adobe CC Packages Fix\" with icon file posix file \"/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns\" buttons {\"OK\"} default button {\"OK\"})"
clickedButton=$( /usr/bin/osascript -e "$runCommand" )
else
echo "No machine name was entered!"
#Tell user that no machine name was entered
runCommand=$( /usr/bin/sw_vers -productVersion)
## Format information #####
displayInfo="You did not type in the computer name, therefore it will be set to what it is currently under 'System Preferences > Sharing > Computer Name:' on JSS. Make sure you either re-run the policy and type in the correct computer name or update it on JSS!"
runCommand="button returned of (display dialog \"$displayInfo\" with title \"Adobe CC Packages Fix\" with icon file posix file \"/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns\" buttons {\"OK\"} default button {\"OK\"})"
clickedButton=$( /usr/bin/osascript -e "$runCommand" )
exit 0
fi
#End the UAPI scripts#
#\\\\\\\\\\\\\\\\\\\\\
# expire the auth token
/usr/bin/curl "$jssurl/uapi/auth/invalidateToken" \
--silent \
--request POST \
--header "Authorization: Bearer $token"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment