Check Computers Against None Site

Rosko
Contributor II
Contributor II

Good Morning JAMF Nation,

Occasionally our imaging team will have a process fail, when this happens the computer ends up in the Site: None, instead of where it should be. As we have our environment broken out into several sites, they can not see into the None site since they are attributed to a specific site. In order to remedy this without them having to constantly come to one of our Mac Admins, I've created this little application below which can be loaded into the JSS as a Self Service Policy and allow whomever you grant access to be able to check if a computer is in the None site and if so, delete it.

#!/bin/bash

#############################################################
## Checks to see if computer is in site = none
## If is in None Site, User is prompted to delete computer.
##
## Name: checkComputerAgainstNone.sh
##
## Created By: Joshua Roskos
## Created On: Monday, December 15th, 2015
#############################################################

#############################################################
## VARIABLES
#############################################################

jssUser="jssuser"
jssPass="jsspass"
jssURL="https://jss.acme.com:8443"

#############################################################
## APPLICATION
#############################################################

## Get Variable(s) From User
/bin/echo "Prompting for ComputerName."
computerName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter the Computer Name in Question:" default answer "usmwksxxxxx" with title "Computer Checker" with text buttons {"Check"} default button 1 with icon 1' -e 'text returned of result')"

## Get Variables From JSS API
computerSite=`/usr/bin/curl -s -u ${jssUser}:${jssPass} ${jssURL}/JSSResource/computers/name/${computerName}/subset/general | /usr/bin/xpath "//computer/general/site/name/text()"  | /usr/bin/sed '$!d'`

computerId=`/usr/bin/curl -s -u ${jssUser}:${jssPass} ${jssURL}/JSSResource/computers/name/${computerName}/subset/general | /usr/bin/xpath "//computer/general/id/text()"  | /usr/bin/sed '$!d'`

## Begin Check
if [[ ${computerSite} == "None" ]]; then
    #PROMPT USER TO CONFIRM IN NONE SITE AND DELETE
    /usr/bin/osascript -e 'Tell application "System Events" to display dialog "'${computerName}' is in the '${computerSite}' Site.
Press Delete to remove '${computerName}' from the '${computerSite}' Site, otherwise Cancel to exit." with title "Remove '${computerName}' from Site?" with text buttons {"Delete", "Cancel"} cancel button "Cancel" default button "Cancel" with icon 2' >/dev/null 2>&1

    if [ $? -eq -0 ]; then
        /usr/bin/curl -s -u ${jssUser}:${jssPass} ${jssURL}/JSSResource/computers/id/${computerId} -X DELETE
    else
        exit 0
    fi

else
    #TELL USER NOT FOUND
    /usr/bin/osascript -e 'Tell application "System Events" to display dialog "'${computerName}' NOT FOUND in None Site!" with text buttons {"OK"} default button "OK" with icon 0'
fi

exit 0

I hope someone finds this as helpful as we did. If you have any questions, don't hesitate to ask.

Josh775fe48d129742a290ce9ba60edc79b1
7441da3491744c5280bba2b7cf308ff0
97aa6efce19a447189a6a1521fa8ba2b
c3a3ce3bdc594b8ca55070887f7247ac

2 REPLIES 2

mm2270
Legendary Contributor III

Hi. Just a quick suggestion. Its possible to only pull the "General" section of a Mac record in the API up front, rather than pulling down the entire API record, then passing it through xpath to only look at the general section. This may help make the script faster and more efficient, though the actual speedup may not be very noticeable.

Example:

curl -sfku ${jssUser}:${jssPass} ${jssURL}/JSSResource/computers/name/${computerName}/subset/general

Rosko
Contributor II
Contributor II

@mm2270 Thanks for the info! I wasn't aware you could do that, but I tested it out and updated above.

Thanks again!