diff --git a/Jamf Pro/Admin Tools/jamf_MoveDevicesToSite.sh b/Jamf Pro/Admin Tools/jamf_MoveDevicesToSite.sh index b1e39d3..fe981bb 100644 --- a/Jamf Pro/Admin Tools/jamf_MoveDevicesToSite.sh +++ b/Jamf Pro/Admin Tools/jamf_MoveDevicesToSite.sh @@ -3,7 +3,7 @@ ################################################################################################### # Script Name: jamf_MoveDevicesToSite.sh # By: Zack Thompson / Created: 4/19/2018 -# Version: 0.7 / Updated: 5/8/2018 / By: ZT +# Version: 0.8 / Updated: 5/8/2018 / By: ZT # # Description: This script allows Site Admins to move devices between Sites that they have perms to. # @@ -118,10 +118,10 @@ for key in objects['sites']: } getComputers() { - echo "Reading in device IDs..." + echo "Reading in Computer IDs..." # Read in the file and assign to variables - while IFS=, read deviceID; do + while IFS=, read -a deviceID; do # PUT changes to the JSS. curlReturn="$(/usr/bin/curl "${curlAPI[@]}" GET ${computersbyID}/${deviceID})" @@ -130,28 +130,40 @@ getComputers() { curlCode=$(echo "$curlReturn" | /usr/bin/awk -F statusCode: '{print $2}') checkStatusCode $curlCode $deviceID - # Regex to get the Site - currentSite=$(echo "$curlReturn" | /usr/bin/sed -e 's/statusCode\:.*//g' | /usr/bin/xmllint --format - | /usr/bin/xpath /computer/site/name 2>/dev/null | LANG=C /usr/bin/sed -e 's/<[^/>]*>//g' | LANG=C /usr/bin/sed -e 's/<[^>]*>/\'$'\n/g') + # Verify the Device exists + if [[ $curlCode != *"200"* ]]; then + continue + fi + # Regex to get the Site + currentSite=$(echo "$curlReturn" | /usr/bin/sed -e 's/statusCode\:.*//g' | /usr/bin/xmllint --format - | /usr/bin/xpath /computer/general/site/name 2>/dev/null | LANG=C /usr/bin/sed -e 's/<[^/>]*>//g' | LANG=C /usr/bin/sed -e 's/<[^>]*>/\'$'\n/g') + + # Verify device is from a site that the Site Admin has permissions too. + if [[ $(echo ${siteNames[@]} | grep -o "${currentSite}") != "${currentSite}" ]]; then + checkStatusCode "SiteError" $deviceID + continue + fi + + # If the Current Site isn't the new Site, move it. if [[ "${currentSite}" != "${selectedSiteName}" ]]; then echo "Reassigning device: ${deviceID} to: ${selectedSiteName}" # PUT changes to the JSS. - curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${computersbyID}/${deviceID} --data "$selectedSiteName")" + curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${computersbyID}/${deviceID} --data "${selectedSiteName}")" # Check if the API call was successful or not. curlCode=$(echo "$curlReturn" | /usr/bin/awk -F statusCode: '{print $2}') checkStatusCode $curlCode $deviceID fi - done < <(echo "${devices}") + done < <(printf '%s\n' "${deviceIDs[@]}") } getMobileDevice() { - echo "Reading in device IDs..." + echo "Reading in Mobile Device IDs..." # Read in the file and assign to variables - while IFS=, read deviceID; do + while IFS=, read -a deviceID; do # PUT changes to the JSS. curlReturn="$(/usr/bin/curl "${curlAPI[@]}" GET ${mobileDevicesByID}/${deviceID})" @@ -160,21 +172,33 @@ getMobileDevice() { curlCode=$(echo "$curlReturn" | /usr/bin/awk -F statusCode: '{print $2}') checkStatusCode $curlCode $deviceID - # Regex to get the Site - currentSite=$(echo "$curlReturn" | /usr/bin/sed -e 's/statusCode\:.*//g' | /usr/bin/xmllint --format - | /usr/bin/xpath /mobileDevice/site/name 2>/dev/null | LANG=C /usr/bin/sed -e 's/<[^/>]*>//g' | LANG=C /usr/bin/sed -e 's/<[^>]*>/\'$'\n/g') + # Verify the Device exists + if [[ $curlCode != *"200"* ]]; then + continue + fi + # Regex to get the Site + currentSite=$(echo "$curlReturn" | /usr/bin/sed -e 's/statusCode\:.*//g' | /usr/bin/xmllint --format - | /usr/bin/xpath /mobileDevice/general/site/name 2>/dev/null | LANG=C /usr/bin/sed -e 's/<[^/>]*>//g' | LANG=C /usr/bin/sed -e 's/<[^>]*>/\'$'\n/g') + + # Verify device is from a site that the Site Admin has permissions too. + if [[ $(echo ${siteNames[@]} | grep -o "${currentSite}") != "${currentSite}" ]]; then + checkStatusCode "SiteError" $deviceID + continue + fi + + # If the Current Site isn't the new Site, move it. if [[ "${currentSite}" != "${selectedSiteName}" ]]; then echo "Reassigning device: ${deviceID} to: ${selectedSiteName}" # PUT changes to the JSS. - curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${mobileDevicesByID}/${deviceID} --data "$selectedSiteName")" + curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${mobileDevicesByID}/${deviceID} --data "${selectedSiteName}")" # Check if the API call was successful or not. curlCode=$(echo "$curlReturn" | /usr/bin/awk -F statusCode: '{print $2}') checkStatusCode $curlCode $deviceID fi - done < <(echo "${devices}") + done < <(printf '%s\n' "${deviceIDs[@]}") } checkStatusCode() {