Posted on 02-09-2017 04:51 PM
Having a little trouble finding the correct operations to pull the Warranty info from the API. Im not even sure if it is possible.
The reason to do this is to have a popup on a computer, which is coming up to warranty end, to inform the end user to prepare their machine for replacement.
If there is a better way to do this please let me know.
Solved! Go to Solution.
Posted on 02-10-2017 10:11 AM
Are you looking to read the "Warranty Expiration" date field from the computer record? Or something else?
If its the Warranty Expiration date (or any other value under the Purchasing section) then that can be read in using the API and an account with read privileges.
Here's a sample of how I would get that date string using the API.
Note that in this script I'm pulling the warranty_expires_epoch
string, rather than just warranty_expires
. The only reason is that if you know how to do the math, it makes it easier to do a comparison against the current time to get a number of days remaining before expiration, or to format the date string for the dialog in any way you want. The regular warranty_expires
string is fine also though if that's all you're looking for.
#!/bin/bash
apiUser="apiuser" ## Edit to an API Username with READ access to Computers
apiPass="apipass" ## Edit to the password for the above API Username
## Get JSS URL from the Mac
jssURL=$(/usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist 2>/dev/null jss_url)
timeEpoch=$(/bin/date +"%s")
## Get the Mac's UUID
UUID=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')
if [[ ! -z "$jssURL" ]]; then
WarrExpDate=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${apiUser}:${apiPass}" "${jssURL}JSSResource/computers/udid/${UUID}" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<warranty_expires_epoch>/{print $3}')
if [[ ! -z "$WarrExpDate" ]]; then
reformat=$((WarrExpDate/1000))
HRDate=$(/bin/date -jf "%s" "$reformat" +"%m-%d-%Y")
daysRemaining=$(($((reformat-timeEpoch))/60/60/24))
if [[ "$daysRemaining" =~ "-" ]]; then
daysLeft=$(echo "$daysRemaining" | sed 's/-//')
word="was"
string="ago"
else
daysLeft="$daysRemaining"
word="is"
string="from now"
fi
echo "Mac's Warranty Expiration Date $word: ${HRDate}, ${daysLeft} days $string"
else
echo "No Warranty Expiration Date found"
exit 0
fi
else
echo "Odd! This Mac may not be managed by the JSS."
exit 1
fi
As a simple example of what this prints out, for a Mac that has an old expiration date, it might print something like:
Mac's Warranty Expiration Date was: 09-13-2015, 516 days ago
For one with a future warranty expiration date, it might show something like:
Mac's Warranty Expiration Date is: 05-01-2017, 79 days from now
You'd need to modify the above to give you some comparisons that make sense for what you want to pop up on screen, then pass that value to a dialog window to show to the user, if appropriate.
Hope this helps get you on the right track. And keep in mind, this is not the only way to do it.
Posted on 02-10-2017 10:11 AM
Are you looking to read the "Warranty Expiration" date field from the computer record? Or something else?
If its the Warranty Expiration date (or any other value under the Purchasing section) then that can be read in using the API and an account with read privileges.
Here's a sample of how I would get that date string using the API.
Note that in this script I'm pulling the warranty_expires_epoch
string, rather than just warranty_expires
. The only reason is that if you know how to do the math, it makes it easier to do a comparison against the current time to get a number of days remaining before expiration, or to format the date string for the dialog in any way you want. The regular warranty_expires
string is fine also though if that's all you're looking for.
#!/bin/bash
apiUser="apiuser" ## Edit to an API Username with READ access to Computers
apiPass="apipass" ## Edit to the password for the above API Username
## Get JSS URL from the Mac
jssURL=$(/usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist 2>/dev/null jss_url)
timeEpoch=$(/bin/date +"%s")
## Get the Mac's UUID
UUID=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')
if [[ ! -z "$jssURL" ]]; then
WarrExpDate=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${apiUser}:${apiPass}" "${jssURL}JSSResource/computers/udid/${UUID}" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<warranty_expires_epoch>/{print $3}')
if [[ ! -z "$WarrExpDate" ]]; then
reformat=$((WarrExpDate/1000))
HRDate=$(/bin/date -jf "%s" "$reformat" +"%m-%d-%Y")
daysRemaining=$(($((reformat-timeEpoch))/60/60/24))
if [[ "$daysRemaining" =~ "-" ]]; then
daysLeft=$(echo "$daysRemaining" | sed 's/-//')
word="was"
string="ago"
else
daysLeft="$daysRemaining"
word="is"
string="from now"
fi
echo "Mac's Warranty Expiration Date $word: ${HRDate}, ${daysLeft} days $string"
else
echo "No Warranty Expiration Date found"
exit 0
fi
else
echo "Odd! This Mac may not be managed by the JSS."
exit 1
fi
As a simple example of what this prints out, for a Mac that has an old expiration date, it might print something like:
Mac's Warranty Expiration Date was: 09-13-2015, 516 days ago
For one with a future warranty expiration date, it might show something like:
Mac's Warranty Expiration Date is: 05-01-2017, 79 days from now
You'd need to modify the above to give you some comparisons that make sense for what you want to pop up on screen, then pass that value to a dialog window to show to the user, if appropriate.
Hope this helps get you on the right track. And keep in mind, this is not the only way to do it.
Posted on 05-17-2023 12:22 AM
@mm2270 So in order to run this API, we should enable GSX connection in Jamf Pro, right?
Posted on 02-11-2017 02:21 AM
Perfect, thanks, I owe you a beer...... or two.
Posted on 02-13-2017 09:51 PM
Is this something you'd run as an EA or as a script and the collect the data someplace else?
Sorry for the odd question just getting started in gathering this kind of information.