Last active
June 22, 2019 00:21
-
-
Save talkingmoose/07f9cf23db74e6f28348ef79e82603cb to your computer and use it in GitHub Desktop.
Look up Jamf Pro serial numbers that may be eligible for Apple's 15-inch MacBook Pro Battery Recall Program. This logs results to a file in the same location as the script. https://support.apple.com/15-inch-macbook-pro-battery-recall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Based on the script by @nicktong found at https://www.jamf.com/jamf-nation/discussions/32400/battery-recall-for-15-mid-2015-mbp | |
| ## Jamf Pro URL and credentials | |
| URL="https://talkingmoose.jamfcloud.com" | |
| username="api-auditor" | |
| password="P@55w0rd" | |
| # pause a number of seconds between each lookup to prevent Apple from detecting automation | |
| throttle="10" | |
| ## File locations | |
| # path to this script | |
| currentDirectory=$( /usr/bin/dirname "$0" ) | |
| # name of this script | |
| currentScript=$( /usr/bin/basename -s .bash "$0" ) | |
| # create log file in same directory as script | |
| logFile="$currentDirectory/$currentScript - $( /bin/date '+%y-%m-%d' ).log" | |
| ## Functions | |
| function logresult() { | |
| if [ $? = 0 ] ; then | |
| /bin/date "+%Y-%m-%d %H:%M:%S $1" >> "$logFile" | |
| else | |
| /bin/date "+%Y-%m-%d %H:%M:%S $2" >> "$logFile" | |
| fi | |
| } | |
| ## Begin script | |
| # start the log | |
| logresult "--------------------- Begin Script ---------------------" | |
| # creating a list of computers without asset tags | |
| logresult "Gathering list of computers meeting criteria." | |
| # human-readable POST XML for a new search | |
| # REMOVE THIRD CRITERION - USED FOR TESTING ONLY | |
| criteriaXML="<advanced_computer_search> | |
| <name>Check MacBook Battery Coverage as of $( /bin/date )</name> | |
| <criteria> | |
| <criterion> | |
| <name>Model Identifier</name> | |
| <priority>0</priority> | |
| <and_or>and</and_or> | |
| <search_type>is</search_type> | |
| <value>MacBookPro11,4</value> | |
| </criterion> | |
| <criterion> | |
| <name>Model Identifier</name> | |
| <priority>1</priority> | |
| <and_or>or</and_or> | |
| <search_type>is</search_type> | |
| <value>MacBookPro11,5</value> | |
| </criterion> | |
| </criteria> | |
| <display_fields> | |
| <size>1</size> | |
| <display_field> | |
| <name>Serial Number</name> | |
| </display_field> | |
| </display_fields> | |
| </advanced_computer_search>" | |
| # flattened XML | |
| flatXML=$( /usr/bin/xmllint --noblanks - <<< "$criteriaXML" ) | |
| # create a temporary advanced computer search in the JSS using the criteria in the XML above | |
| createSearch=$( /usr/bin/curl $URL/JSSResource/advancedcomputersearches/id/0 \ | |
| --silent \ | |
| --user "$username:$password" \ | |
| --header "Content-Type: text/xml" \ | |
| --request POST \ | |
| --data "$flatXML" ) | |
| # log the result | |
| logresult "Created temporary Advanced Computer Search in JSS at $URL." "Failed creating temporary Advanced Computer Search in JSS at $URL." | |
| # get temporary advanced computer search ID | |
| searchID=$( /usr/bin/xpath '/advanced_computer_search/id/text()' <<< "$createSearch" ) | |
| # run the search and return serial numbers | |
| search=$( /usr/bin/curl $URL/JSSResource/advancedcomputersearches/id/$searchID \ | |
| --silent \ | |
| --user "$username:$password" \ | |
| --header "Accept: text/xml" \ | |
| --request GET ) | |
| # turn the returned list into a list of just serial numbers (dirty little secret) | |
| serialNumberList=$( echo "$search" | /usr/bin/perl -lne 'BEGIN{undef $/} while (/<Serial_Number>(.*?)<\/Serial_Number>/sg){print $1}' ) | |
| # count the serial numbers and log the results | |
| serialNumberCount=$( echo $serialNumberList | /usr/bin/wc -w | xargs ) | |
| # log the result | |
| logresult "Found $serialNumberCount computers." "Failed finding any computers." | |
| # delete the temporary advanced computer search | |
| /usr/bin/curl $URL/JSSResource/advancedcomputersearches/id/$searchID \ | |
| --silent \ | |
| --user "$username:$password" \ | |
| --request DELETE | |
| # log the result | |
| logresult "Deleted temporary Advanced Computer Search." "Failed deleting temporary Advanced Computer Search." | |
| for aSerialNumber in $serialNumberList | |
| do | |
| ApplePostURL="https://qualityprograms.apple.com/snlookup/062019" | |
| quotedSerialNumber="\"$aSerialNumber\"" | |
| quotedUUID='"'$(uuidgen | tr "[:upper:]" "[:lower:]")'"' | |
| ApplePostData="'{\"serial\":$quotedSerialNumber,\"GUID\":$quotedUUID}'" | |
| echo ApplePostURL is $ApplePostURL | |
| response=$( /usr/bin/curl \ | |
| --data "$ApplePostData" \ | |
| --header "Content-Type: application/json" \ | |
| --request POST "$ApplePostURL" ) | |
| if [[ "$response" == *'"status":"E00"'* ]]; then | |
| logresult "$aSerialNumber: E00-Eligible" | |
| elif [[ "$response" == *'"status":"E01"'* ]]; then | |
| logresult "$aSerialNumber: E01-Ineligible" | |
| elif [[ "$response" == *'"status":"E99"'* ]]; then | |
| logresult "$aSerialNumber: E99-ProcessingError" | |
| elif [[ "$response" == *'"status":"FE01"'* ]]; then | |
| logresult "$aSerialNumber: FE01-EmptySerial" | |
| elif [[ "$response" == *'"status":"FE02"'* ]]; then | |
| logresult "$aSerialNumber: FE02-InvalidSerial" | |
| elif [[ "$response" == *'"status":"FE03"'* ]]; then | |
| logresult "$aSerialNumber: FE03-ProcessingError" | |
| else | |
| logresult "$aSerialNumber: Err1-UnexpectedResponse" | |
| fi | |
| done | |
| ## Finish script | |
| logresult "Completing script." | |
| logresult "---------------------- End Script ---------------------- | |
| " | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment