Need API help- filling extension attribute text field

The_Monger
New Contributor II

Hey gang-

 

Thought I had this one locked up, but was woefully incorrect. I'm trying to put something in Self Service that allows a tech to input a serial number and ticket number, and writes that into an extension attribute for the record.

Everything's fine until it comes to populating the EA, then it returns error code 400. I'm assuming the format for the EA input is wrong.

I've seen a few articles doing the same thing on JAMFNation, but they're a little on the old side, and some of them have unanswered questions.

Here's what I have so far:

 

#get device serial number
serialNumber="$(osascript -e 'display dialog "Please enter the Serial Number" default answer "" with title "Serial Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "serial number entered is $serialNumber"

#get the ticket number
ticketNumber="$(osascript -e 'display dialog "Please enter the Ticket Number" default answer "" with title "Service Now Ticket Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "ticket number entered is $ticketNumber"

echo "<computer><extension_attributes><extention_attribute><id>$eaID</id><name>$eaName</name><type>String</type><multi_value>false</multi_value><value>$ticketNumber</value></extension_attribute></extension_attributes></computer>" | curl -X PUT -fku $jssUser:$jssPass -d @- "$jssURL/JSSResource/computers/serialnumber/$serialNumber/subset/extension_attributes" -H "Content-Type: text/xml"

 

 

1 ACCEPTED SOLUTION

mdp
Contributor

My quick go at it — seems to work. Potentially has the issue with the Jamf username/password hardcoded into the script (depending how it's being used), but you can always ask for those via Applescript as well.

 

#!/bin/bash
#Matthew Prins 2022
#https://github.com/MatthewPrins/Jamf/

######################
#Editable variables

#Jamf credentials
username="xxxxxx"
password="xxxxxx"
url="https://xxxxxx.jamfcloud.com"

#Computer Extension Attribute number -- found in URL on group's page
extensionattributeid="7"

######################

#Token function -- based on https://developer.jamf.com/jamf-pro/docs/jamf-pro-api-overview
getBearerToken() {
	response=$(curl -s -u "$username":"$password" "$url"/api/v1/auth/token -X POST)
	bearerToken=$(echo "$response" | plutil -extract token raw -)
	tokenExpiration=$(echo "$response" | plutil -extract expires raw - | awk -F . '{print $1}')
	tokenExpirationEpoch=$(date -j -f "%Y-%m-%dT%T" "$tokenExpiration" +"%s")
}

#get token
getBearerToken

#get device serial number
serialNumber="$(osascript -e 'display dialog "Please enter the Serial Number" default answer "" with title "Serial Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "serial number entered is $serialNumber"

#get the ticket number
ticketNumber="$(osascript -e 'display dialog "Please enter the Ticket Number" default answer "" with title "Service Now Ticket Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "ticket number entered is $ticketNumber"

curl --silent \
--request PUT \
--url $url/JSSResource/computers/serialnumber/$serialNumber \
--header "Authorization: Bearer $bearerToken" \
--header 'Accept: application/xml' \
--header 'Content-Type: application/xml' \
--data "<computer><extension_attributes><extension_attribute><id>$extensionattributeid</id><value>$ticketNumber</value></extension_attribute></extension_attributes></computer>"

 

 

---
Matthew Prins -- Jamf Scripts @ Github

View solution in original post

6 REPLIES 6

pbenware1
Release Candidate Programs Tester

Don't have an answer, but a rather a question about the serial # dialog piece (not criticizing, just curious);

What's the reasoning for having the tech manually enter the Serial?  If this is running from Self Service, you could have the script collect the serial into a variable and pass that without needing the serial # dialog piece.

Valid question- the idea is that a tech runs it from their Mac, so that it doesn't have to be scoped to a user. This also cuts down on giving unnecessary access to the JSS. So far it's been a hit. We have a Self Heal in there, too.

pbenware1
Release Candidate Programs Tester

Ah.  makes sense.  Thank you.

 

mdp
Contributor

My quick go at it — seems to work. Potentially has the issue with the Jamf username/password hardcoded into the script (depending how it's being used), but you can always ask for those via Applescript as well.

 

#!/bin/bash
#Matthew Prins 2022
#https://github.com/MatthewPrins/Jamf/

######################
#Editable variables

#Jamf credentials
username="xxxxxx"
password="xxxxxx"
url="https://xxxxxx.jamfcloud.com"

#Computer Extension Attribute number -- found in URL on group's page
extensionattributeid="7"

######################

#Token function -- based on https://developer.jamf.com/jamf-pro/docs/jamf-pro-api-overview
getBearerToken() {
	response=$(curl -s -u "$username":"$password" "$url"/api/v1/auth/token -X POST)
	bearerToken=$(echo "$response" | plutil -extract token raw -)
	tokenExpiration=$(echo "$response" | plutil -extract expires raw - | awk -F . '{print $1}')
	tokenExpirationEpoch=$(date -j -f "%Y-%m-%dT%T" "$tokenExpiration" +"%s")
}

#get token
getBearerToken

#get device serial number
serialNumber="$(osascript -e 'display dialog "Please enter the Serial Number" default answer "" with title "Serial Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "serial number entered is $serialNumber"

#get the ticket number
ticketNumber="$(osascript -e 'display dialog "Please enter the Ticket Number" default answer "" with title "Service Now Ticket Number" giving up after 86400 with text buttons {"OK"} default button 1' -e 'return text returned of result')"
echo "ticket number entered is $ticketNumber"

curl --silent \
--request PUT \
--url $url/JSSResource/computers/serialnumber/$serialNumber \
--header "Authorization: Bearer $bearerToken" \
--header 'Accept: application/xml' \
--header 'Content-Type: application/xml' \
--data "<computer><extension_attributes><extension_attribute><id>$extensionattributeid</id><value>$ticketNumber</value></extension_attribute></extension_attributes></computer>"

 

 

---
Matthew Prins -- Jamf Scripts @ Github

The_Monger
New Contributor II

This did the trick! Thanks much!

Joostvantwout
New Contributor III

Below script will work to adjust a static EA value of choice.

Let me know in case of questions but this should do the trick.

#!/bin/sh

Serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
jamfUser=TO FILL
jamfPass=TO FILL
jamfUrl=TO FILL

# Get JAMF computer ID
MacID=$(curl -H "Accept: text/xml" -sfku "${jamfUser}:${jamfPass}" "${jamfUrl}/JSSResource/computers/serialnumber/$Serial/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<id>/{print $3}')

Echo $MacID
Echo $Serial

# Update JAMF Extension Attribute to "VALUE"
curl -sku $jamfUser:$jamfPass $jamfUrl/JSSResource/computers/id/$MacID -X PUT -H "Content-type: application/xml" -d "<computer><extension_attributes><extension_attribute><name>NAME OF EA</name><value>VALUE TO CHANGE</value></extension_attribute></extension_attributes></computer>"

jamf recon