Warranty API

BOBW
Contributor II

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.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

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.

View solution in original post

4 REPLIES 4

mm2270
Legendary Contributor III

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.

@mm2270 So in order to run this API, we should enable GSX connection in Jamf Pro, right?

BOBW
Contributor II

Perfect, thanks, I owe you a beer...... or two.

daniel_ross
Contributor III

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.