-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPopulateMobileDeviceAssetTags.sh
More file actions
executable file
·219 lines (159 loc) · 6.55 KB
/
PopulateMobileDeviceAssetTags.sh
File metadata and controls
executable file
·219 lines (159 loc) · 6.55 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
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
#!/bin/bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Written by: William Smith
# Professional Services Engineer
# Jamf
# bill@talkingmoose.net
# https://github.com/talkingmoose/Jamf-Scripts
#
# Originally posted: April 21, 2015
# Last updated: August 13, 2018
#
# Purpose: Searches a Jamf Pro server for mobile device assets
# with empty Asset Tag fields and populates those fields from lists
# provided by Apple. Script relies on the Jamf Pro Classic API. Requires
# a text file of device serial numbers and asset tags in the format
# "SerialNumber > tab > AssetTag > return" and named FullList.txt in
# the same directory as the script.
#
# The script creates a log file in the same folder as the script.
#
# Except where otherwise noted, this work is licensed under
# http://creativecommons.org/licenses/by/4.0/
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
####################################
# start the timer
####################################
# the time right now
startTime=$( /bin/date '+%s' )
####################################
# Jamf Pro URL and credentials
####################################
URL="https://jamfpro.domain.com:8443"
userName="API-Editor"
passWord="password"
####################################
# File locations
####################################
# path to this script
currentDirectory=$( /usr/bin/dirname "$0" )
# name of this script
currentScript=$( /usr/bin/basename -s .sh "$0" )
# set the log file in same directory as script
logFile="$currentDirectory/$currentScript - $( /bin/date '+%y-%m-%d' ).log"
# store Apple-provided spreadsheets file in same directory as script
fullList=$( /bin/cat "$currentDirectory/FullList.txt" )
####################################
# Functions
####################################
function stripreturns() {
stripped=$( /bin/echo $1 | /usr/bin/xmllint --noblanks - )
/bin/echo $stripped
}
function logresult() {
if [ $? = 0 ] ; then
/bin/date "+%Y-%m-%d %H:%M:%S $1" >> "$logFile"
else
/bin/date "+%Y-%m-%d %H:%M:%S $2" >> "$logFile"
fi
}
####################################
# Create list of computer
# serial numbers without asset tags
####################################
# start the log
logresult "--------------------- Begin Script ---------------------"
# rotate logs -- delete all but the five most recent log files
deleteOldLogs=$( /bin/ls -1t "$currentDirectory/$currentScript"*.log | /usr/bin/tail -n +6 )
while IFS= read -r aLine
do
logFileName=$( /usr/bin/basename "$aLine" )
/bin/rm "$aLine"
logresult "Deleting old log file: $logFileName."
done <<< "$deleteOldLogs"
# creating a list of computers without asset tags
logresult "Gathering list of computers without asset tags."
# human-readable POST XML for a new search
TEHxml="<advanced_mobile_device_search>
<name>Mobile Devices with no asset tags as of $( /bin/date )</name>
<criteria>
<criterion>
<name>Asset Tag</name>
<priority>0</priority>
<and_or>and</and_or>
<search_type>is</search_type>
<value></value>
</criterion>
</criteria>
<display_fields>
<display_field>
<name>Serial Number</name>
</display_field>
</display_fields>
</advanced_mobile_device_search>"
# this strips the returns before POSTing the XML
POSTxml=$( stripreturns "$TEHxml" )
# create a temporary advanced computer search in the Jamf Pro server using the criteria in the POST XML above
createSearch=$( /usr/bin/curl -k -s 0 $URL/JSSResource/advancedmobiledevicesearches/id/0 --user "$userName:$passWord" -H "Content-Type: text/xml" -X POST -d "$POSTxml" )
# log the result
logresult "Created temporary Advanced Mobile Device Search in Jamf Pro at $URL." "Failed creating temporary Advanced Mobile Device Search in Jamf Pro at $URL."
# get temporary advanced computer search ID
searchID=$( /bin/echo $createSearch | /usr/bin/awk -F "<id>|</id>" '{ print $2 }' )
# run the search and return serial numbers
search=$( /usr/bin/curl -k -s 0 $URL/JSSResource/advancedmobiledevicesearches/id/$searchID --user "$userName:$passWord" -H "Accept: text/xml" -X GET )
# turn the returned list into a list of just serial numbers
serialNumberList=$( /bin/echo "$search" | /usr/bin/perl -lne 'BEGIN{undef $/} while (/<Serial_Number>(.*?)<\/Serial_Number>/sg){print $1}' )
# count the serial numbers and log the results
serialNumberCount=$( /bin/echo $serialNumberList | /usr/bin/wc -w )
serialNumberCount=$( stripreturns "$serialNumberCount" )
# log the result
logresult "Found $serialNumberCount mobile devices without asset tags." "Failed finding mobile devices without asset tags."
# delete the temporary advanced computer search
/usr/bin/curl -k -s 0 $URL/JSSResource/advancedmobiledevicesearches/id/$searchID --user "$userName:$passWord" -X DELETE
# log the result
logresult "Deleted temporary Advanced Mobile Device Search." "Failed deleting temporary Advanced Computer Search."
####################################
# Compare Apple's list of serial
# numbers with the found list of
# serial numbers. Populate asset
# tags for serial numbers that
# have no asset tag.
####################################
for aLine in $serialNumberList
do
matchedDevice=$( echo "$fullList" | grep "$aLine" )
if [ "$matchedDevice" = "" ] ; then
# log the result
logresult "Serial number $aLine not found in the spreadsheet from Apple."
else
serialNumber=$( /bin/echo "$matchedDevice" | /usr/bin/awk '{ print $1 }' )
assetTag=$( /bin/echo "$matchedDevice" | /usr/bin/awk '{ print $2 }' )
THExml="<mobile_device>
<general>
<asset_tag>$assetTag</asset_tag>
</general>
</mobile_device>"
PUTxml=$( stripreturns "$THExml" )
/usr/bin/curl -k 0 $URL/JSSResource/mobiledevices/serialnumber/$serialNumber --user "$userName:$passWord" -H "Content-Type: text/xml" -X PUT -d "$PUTxml"
# log the result
logresult "Added asset tag $assetTag to mobile device with serial number $serialNumber." "Failed to add asset tag $assetTag to mobile device with serial number $serialNumber."
# keep count of populated asset tags
matched=$((matched+1))
fi
done
####################################
# stop the timer
# calculate how long the script ran
####################################
logresult "Completing script."
logresult "Populated $matched asset tags of $serialNumberCount mobile devices without asset tags."
# the time right now
stopTime=$( /bin/date '+%s' )
# subtract start time from stop time and log the time in seconds
DIFF=$(($stopTime-$startTime))
logresult "Script operations took $DIFF seconds to complete."
logresult "---------------------- End Script ----------------------
"
exit 0