-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployiOSAtSchool.sh
executable file
·255 lines (219 loc) · 11.5 KB
/
deployiOSAtSchool.sh
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
######################### Variables #########################
apiUser=$3
apiUserPass=$4
####################### Do Not Modify #######################
# function for error reporting
abort() {
errorString=${*}
echo "ERROR: $errorString"
exit 1
}
# explanation of script
if [[ ${#} -lt 2 ]] || [[ ${*} == *"-h"* ]] || [[ ${*} == *"--help"* ]]; then
echo "Usage: $0 https://jss.example.com:8443 /path/to/example.csv [username] [password]"
echo ""
echo "CSV should have 8 columns with the following headers:
1. username (username) [required]
2. full-name (full name) [required]
3. email (email address) [required]
4. apple-id (apple id) [can be left empty]
5. grad-year (grad year of student) [can be left empty]
6. stream (for A/B schools) [can be left empty]
7. position (student || teacher || staff) [required]
8. serial-number (device serial number) [required]"
exit 1
fi
# set jssURL to first parameter
jssURL=$1
# if jssURL is emply, warn user
if [[ -z "${jssURL}" ]]; then
abort "Please specify a JSS server"
elif [[ `curl --connect-timeout 10 -k -sS $jssURL/healthCheck.html -w \\nStatus:\ %{http_code} | grep Status: | awk '{print $2}'` != 200 ]]; then
abort "Could not connect to JSS server $jssURL"
fi
# set csvFile to second parameter
csvFile=$2
# if csvFile is empty, warn user
if [[ -z "${csvFile}" ]]; then
abort "Please specify a CSV file"
fi
# if csvFile cannot be read, warn user
if [[ ! -r "${csvFile}" ]]; then
abort "Cannot read the CSV file"
fi
# collect username if not specified
if [[ -z "${apiUser}" ]]; then
echo "JSS Username: "
read apiUser
fi
# collect username if not specified
if [[ -z "${apiUserPass}" ]]; then
echo "JSS Password: "
read -s apiUserPass
fi
# test supplied details
testCredentials=$(curl --connect-timeout 10 -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/accounts" -w \\nStatus:\ %{http_code} | grep Status: | awk '{print $2}')
if [[ "$testCredentials" == "200" ]]; then
echo "Credentials look good, moving forward..."
else
abort "The user account or password was wrong, or doesn't have API Rights"
fi
# find ID of device
findDeviceID() {
deviceID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledevices/serialnumber/$serialNumber" -H "Accept: application/xml" | xpath '/mobile_device/general/id/text()' 2>/dev/null)
}
# find ID of user
findUserID() {
userID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/users/name/$userName" -H "Accept: application/xml" | xpath 'user/id/text()' 2>/dev/null)
}
# find ID of 'Managed Apple ID' Extension Attribute
findManagedAppleIDEA() {
managedAppleIDEAID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/name/Managed%20Apple%20ID" -H "Accept: application/xml" | xpath 'user_extension_attribute/id/text()' 2>/dev/null)
}
# create 'Managed Apple ID' Extension Attribute
createManagedAppleIDEA() {
postXML="<user_extension_attribute><name>Managed Apple ID</name><data_type>String</data_type><input_type><type>Text Field</type></input_type></user_extension_attribute>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" /dev/null 2>&1
}
# find ID of 'Grad Year' Extension Attribute
findUserGradYearEA() {
userGradYearEAID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/name/Grad%20Year" -H "Accept: application/xml" | xpath 'user_extension_attribute/id/text()' 2>/dev/null)
}
# create 'Grad Year' Extension Attribute
createUserGradYearEA() {
postXML="<user_extension_attribute><name>Grad Year</name><data_type>Integer</data_type><input_type><type>Text Field</type></input_type></user_extension_attribute>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" > /dev/null 2>&1
}
# find ID of 'Grad Year' Extension Attribute
findDeviceGradYearEA() {
deviceGradYearEAID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledeviceextensionattributes/name/Grad%20Year" -H "Accept: application/xml" | xpath 'mobile_device_extension_attribute/id/text()' 2>/dev/null)
}
# create 'Grad Year' Extension Attribute
createDeviceGradYearEA() {
postXML="<mobile_device_extension_attribute><name>Grad Year</name><data_type>Integer</data_type><input_type><type>Text Field</type></input_type><inventory_display>User and Location</inventory_display></mobile_device_extension_attribute>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledeviceextensionattributes/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" > /dev/null 2>&1
}
# find ID of 'Stream' Extension Attribute
findUserStreamEA() {
userStreamEAID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/name/Stream" -H "Accept: application/xml" | xpath 'user_extension_attribute/id/text()' 2>/dev/null)
}
# create 'Stream' Extension Attribute
createUserStreamEA() {
postXML="<user_extension_attribute><name>Stream</name><data_type>String</data_type><input_type><type>Pop-up Menu</type><popup_choices><choice>A</choice><choice>B</choice><choice>NA</choice></popup_choices></input_type></user_extension_attribute>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/userextensionattributes/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" > /dev/null 2>&1
}
# find ID of 'Stream' Extension Attribute
findDeviceStreamEA() {
deviceStreamEAID=$(curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledeviceextensionattributes/name/Stream" -H "Accept: application/xml" | xpath 'mobile_device_extension_attribute/id/text()' 2>/dev/null)
}
# create 'Stream' Extension Attribute
createDeviceStreamEA() {
postXML="<mobile_device_extension_attribute><name>Stream</name><data_type>String</data_type><input_type><type>Pop-up Menu</type><popup_choices><choice>A</choice><choice>B</choice><choice>NA</choice></popup_choices></input_type><inventory_display>User and Location</inventory_display></mobile_device_extension_attribute>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledeviceextensionattributes/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" > /dev/null 2>&1
}
updateDeviceName() {
# this changes the name of the Device
fullDeviceName=$(echo "$fullName" | sed 's/ /%20/g')
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledevicecommands/command/DeviceName/$fullDeviceName/id/$deviceID" -X POST > /dev/null 2>&1
# this pushes a inventory for it
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledevicecommands/command/UpdateInventory/id/$deviceID" -X POST > /dev/null 2>&1
# this pushes a blankpush
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledevicecommands/command/BlankPush/id/$deviceID" -X POST > /dev/null 2>&1
}
# update username, full name, and position of user within device inventory
updateDeviceInfo() {
putXML="<mobile_device><location><username>$userName</username><real_name>$fullName</real_name><position>$position</position></location><extension_attributes><extension_attribute><id>$deviceGradYearEAID</id><value>$gradYear</value></extension_attribute><extension_attribute><id>$deviceStreamEAID</id><value>$stream</value></extension_attribute></extension_attributes></mobile_device>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/mobiledevices/id/$deviceID" -H "Content-Type: text/xml" -X PUT -d "$putXML" > /dev/null 2>&1
}
# create new user and apply position, grad year, and assigned device
createUser() {
postXML="<user><name>$userName</name><full_name>$fullName</full_name><email>$email</email><position>$position</position><extension_attributes><extension_attribute><id>$userGradYearEAID</id><value>$gradYear</value></extension_attribute><extension_attribute><id>$userStreamEAID</id><value>$stream</value></extension_attribute><extension_attribute><id>$managedAppleIDEAID</id><value>$appleID</value></extension_attribute></extension_attributes></user>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/users/id/0" -H "Content-Type: text/xml" -X POST -d "$postXML" > /dev/null 2>&1
}
# update position, grad year, and assigned device
updateUserInfo() {
putXML="<user><full_name>$fullName</full_name><email>$email</email><position>$position</position><extension_attributes><extension_attribute><id>$userGradYearEAID</id><value>$gradYear</value></extension_attribute><extension_attribute><id>$userStreamEAID</id><value>$stream</value></extension_attribute><extension_attribute><id>$managedAppleIDEAID</id><value>$appleID</value></extension_attribute></extension_attributes></user>"
curl -k -sS -u "$apiUser":"$apiUserPass" "$jssURL/JSSResource/users/id/$userID" -H "Content-Type: text/xml" -X PUT -d "$putXML" > /dev/null 2>&1
}
# find ID or create 'Managed Apple ID' Extension Attribute
findManagedAppleIDEA
if [[ -z $managedAppleIDEAID ]]; then
createManagedAppleIDEA
findManagedAppleIDEA
echo "Created 'Managed Apple ID' User Extension Attribute with ID: $managedAppleIDEAID"
fi
# find ID or create 'Grad Year' User Extension Attribute
findUserGradYearEA
if [[ -z $userGradYearEAID ]]; then
createUserGradYearEA
findUserGradYearEA
echo "Created 'Grad Year' User Extension Attribute with ID: $userGradYearEAID"
fi
# find ID or create 'Grad Year' Mobile Device Extension Attribute
findDeviceGradYearEA
if [[ -z $deviceGradYearEAID ]]; then
createDeviceGradYearEA
findDeviceGradYearEA
echo "Created 'Grad Year' Mobile Device Extension Attribute with ID: $deviceGradYearEAID"
fi
# find ID or create 'Stream' User Extension Attribute
findUserStreamEA
if [[ -z $userStreamEAID ]]; then
createUserStreamEA
findUserStreamEA
echo "Created 'Stream' User Extension Attribute with ID: $userStreamEAID"
fi
# find ID or create 'Stream' Mobile Device Extension Attribute
findDeviceStreamEA
if [[ -z $deviceStreamEAID ]]; then
createDeviceStreamEA
findDeviceStreamEA
echo "Created 'Stream' Mobile Device Extension Attribute with ID: $deviceStreamEAID"
fi
# remove first line of csvFile
csvFileWithoutHeader=/tmp/rename_iOS_from-tmp.csv
echo "Removing headers from CSV..."
tr -d $'\r' < "$csvFile" | awk 'NR>1' > $csvFileWithoutHeader
# all the things
while IFS=, read userName fullName email appleID gradYear stream position serialNumber
do
if [[ -z $userName ]] || [[ -z $fullName ]] || [[ -z $email ]] || [[ -z $position ]] || [[ -z $serialNumber ]]; then
echo "Required info missing. Skipping... Username: $userName Fullname: $fullName Email: $email Stream: $gradYear Position: $position Serial Number: $serialNumber"
else
if [[ -z $appleID ]]; then
appleID=$email
fi
if [[ -z $gradYear ]]; then
gradYear=0000
fi
if [[ -z $stream ]]; then
stream=NA
elif [[ $stream == a ]]; then
stream=A
elif [[ $stream == b ]]; then
stream=B
fi
echo "Processing $fullName..."
findDeviceID
if [[ -z $deviceID ]]; then
echo "Serial Number $serialNumber not found, skipping..."
else
findUserID
if [[ -z $userID ]]; then
echo "Username $userName not found, creating account now..."
createUser
updateDeviceName
updateDeviceInfo
else
updateUserInfo
updateDeviceName
updateDeviceInfo
fi
fi
fi
done < $csvFileWithoutHeader
echo "Finished processing $csvFile"
# cleanup
rm -f $csvFileWithoutHeader
exit 0