v0.8 = Improved logic within the Get Device Functions

+ Added logic to verify the device exists
 + Added logic to verify the device is in a Site the Site Admin has permissions too
 + Fixed missing XML elements
This commit is contained in:
Zack T
2018-05-08 14:48:39 -07:00
parent d9f29b268b
commit 8d28458609

View File

@@ -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 "<computer><site><name>$selectedSiteName</name></site></computer>")"
curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${computersbyID}/${deviceID} --data "<computer><general><site><name>${selectedSiteName}</name></site></general></computer>")"
# 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 "<mobileDevice><site><name>$selectedSiteName</name></site></mobileDevice>")"
curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${mobileDevicesByID}/${deviceID} --data "<mobileDevice><general><site><name>${selectedSiteName}</name></site></general></mobileDevice>")"
# 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() {