-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJamfComputerDelete
More file actions
126 lines (114 loc) · 5.21 KB
/
JamfComputerDelete
File metadata and controls
126 lines (114 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# API script to delete devices from Jamf Pro server
# This is designed to be run locally as a logged in user with the terminal (or other similar) application.
# This can create a preference file for future use for each Jamf Pro Server.
# A single Jamf Pro Server parameter is required and will exit with message if not provided.
# Created by Dan Engh and added to GitHub 9/16/2019
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
## Variables ##
# Do not change these
if [[ -z $1 ]]; then
read -p "Enter a Jamf Pro host name. example: myjss.company.com: " jssEntry
if [[ "$jssEntry" == "" ]]; then
echo "Jamf server not recognized. Please provide a Jamf URL host name. example: myjss.company.com"
exit 1
else
jss="https://"$jssEntry":8443/JSSResource"
if [[ -e ~/Library/Preferences/com.mycompany."$jssEntry".plist ]]; then
user=$( defaults read com.mycompany."$jssEntry".plist api_user )
pw=$( defaults read com.mycompany."$jssEntry".plist api_pass )
else
read -p "API Username: " user
read -p "API Password: " pw
read -p "Would you like to save these for later [y/n]? " createPlist
if [[ "$createPlist" == "y" ]]; then
defaults write com.mycompany."$jssEntry".plist api_user "$user"
defaults write com.mycompany."$jssEntry".plist api_pass "$pw"
echo "Preference file com.mycompany."$jssEntry".plist created"
fi
fi
fi
else
jss="https://"$1":8443/JSSResource"
if [[ -e ~/Library/Preferences/com.mycompany."$1".plist ]]; then
user=$( defaults read com.mycompany."$1".plist api_user )
pw=$( defaults read com.mycompany."$1".plist api_pass )
else
read -p "API Username: " user
read -p "API Password: " pw
read -p "Would you like to save these for later [y/n]? " createPlist
if [[ "$createPlist" == "y" ]]; then
defaults write com.mycompany."$1".plist api_user "$user"
defaults write com.mycompany."$1".plist api_pass "$pw"
echo "Preference file com.mycompany."$1".plist created"
fi
fi
fi
currentUser=$( ls -l /dev/console | awk '{print $3}' )
compPath="/computers/serialnumber/"
mobDevPath="/mobiledevices/serialnumber/"
logLocation="/Users/"$currentUser"/Library/Logs"
fileLocation="/Users/"$currentUser"/Desktop/"
currDate=$(Date)
# Serial numbers can be added directly to the array below but will otherwise start as empty.
deviceList=(
)
# create log file
if [[ -z "$logLocation"/Deleted_Devices.log ]]; then
touch "$logLocation"/Deleted_Devices.log
fi
echo "$currDate" >> "$logLocation"/Deleted_Devices.log
# Check to see if there is a file that we are reading from. If no file input, check to see if the array has been modified directly.
# If no file and array is empty, script will exit as successfully run.
read -p "Enter csv filename including the .csv extension. File should be located on the Desktop. Hit enter if no file: " fileToUse
if [[ "$fileToUse" == "" ]]; then
echo "No file to use as input. Trying array...."
if [[ ${#deviceList} == 0 ]]; then
read -p "Would you like to delete a single device [y/n]? " singleDelete
if [[ "$singleDelete" == "y" ]]; then
read -p "Enter serial number: " singleSerial
deviceList=("$singleSerial")
else
echo "No devices to remove. Exiting."
exit 0
fi
fi
else
fileToDelete="$fileLocation""$fileToUse"
if [[ -e "$fileToDelete" ]]; then
deviceList=( $(cut -d "," -f1 "$fileToDelete") )
else
echo "File doesn't exist. Check the file name and make sure it is located on the desktop."
exit 1
fi
fi
for item in ${deviceList[@]}; do
echo "$item"
done
for device in "${deviceList[@]}"; do
compExists=$( curl -s -k -u "$user":"$pw" -H "Accept: application/xml" "$jss""$compPath""$device" | xpath "/computer/general/name" | sed -e 's/<[^>]*>//g' )
mobDevExists=$( curl -s -k -u "$user":"$pw" -H "Accept: application/xml" "$jss""$mobDevPath""$device" | xpath "/mobile_device/general/name" | sed -e 's/<[^>]*>//g' )
if [[ "$compExists" != "" ]]; then
# echo "curl -k -u "$user":"$pw" "$jss""$compPath""$device" -X DELETE"
curl -k -u "$user":"$pw" "$jss""$compPath""$device" -X DELETE > /dev/null 2>&1
echo ""$device" - "$compExists" has been deleted (Computer)" >> "$logLocation"/Deleted_Devices.log
elif [[ "$mobDevExists" != "" ]]; then
# echo "curl -k -u "$user":"$pw" "$jss""$mobDevPath""$device" -X DELETE"
curl -k -u "$user":"$pw" "$jss""$mobDevPath""$device" -X DELETE > /dev/null 2>&1
echo ""$device" - "$mobDevExists" has been deleted (Mobile Device)" >> "$logLocation"/Deleted_Devices.log
else
echo ""$device" is not in Jamf" >> "$logLocation"/Deleted_Devices.log
fi
done
echo ""
echo ""
echo "Log file is located at "$logLocation"/Deleted_Devices.log"