-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmigration-tool.sh
executable file
·1246 lines (1046 loc) · 57.4 KB
/
migration-tool.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# JAMF MIGRATION TOOL
# A script to either save JSS config via api to XML and/or upload parsed XML to a JSS.
#
# Original Author of JSS-Config-In-A-Box:
# https://github.com/franton/JSS-Config-In-A-Box
#
# JSS-Config-In-A-Box was loosely based on the work by Jeffrey Compton:
# https://github.com/igeekjsc/JSSAPIScripts/blob/master/jssMigrationUtility.bash
#
# Adapted for new purposes by Graham Pugh @ ETH Zurich.
#
# Adapted again for integration in the multitenant-jamf-tools repo by Graham Pugh @ JAMF.
# NOTE: unlike some of the other multitenant-jamf-tools, this script can only apply changes to one destination instance per run.
# -------------------------------------------------------------------------
# Source the file for obtaining the token and setting the server
# -------------------------------------------------------------------------
# source the _common-framework.sh file
# shellcheck source-path=SCRIPTDIR source=_common-framework.sh
source "_common-framework.sh"
if [[ ! -d "${this_script_dir}" ]]; then
echo "ERROR: path to repo ambiguous. Aborting."
exit 1
fi
# -------------------------------------------------------------------------
# Set variables
# -------------------------------------------------------------------------
# Other fixed variables
xmlloc_default="/Users/Shared/Jamf/jamf-api-archive"
log_file="$HOME/Library/Logs/JAMF/migration-tool.log"
git_branch="tst-template"
icons_folder="$xmlloc_default/icons"
# reduce the curl tries
max_tries_override=2
# -------------------------------------------------------------------------
# Declare the API endpoints that will be used in read/wipe/write
# -------------------------------------------------------------------------
# This script relies on the following files, which contain a list of all the API endpoints.
# Each endpoint can be commented in or out, depending on what you wish to copy.
# Note that different files are necessary because the order has to be slightly different for reading and writing.
templates_folder="${this_script_dir}/migration-tool-templates"
readfile="${templates_folder}/read_all.txt"
readlimitedfile="${templates_folder}/read_limited.txt"
wipefile="${templates_folder}/wipe_all.txt"
wipelimitedfile="${templates_folder}/wipe_limited.txt"
writefile="${templates_folder}/write_all.txt"
writelimitedfile="${templates_folder}/write_limited.txt"
writeiosfile="${templates_folder}/write_ios.txt"
# -------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------
check_xml_folder() {
# Determine the storage directory
if [[ ! "$xmlloc" ]]; then
xmlloc="$xmlloc_default"
fi
# Check and create the JSS xml folder and archive folders if missing
if [[ ! -d "$xmlloc" ]]; then
mkdir -p "$xmlloc"
else
# if the storage directory is a git archive, make sure it's synced
if [[ -d "$xmlloc/.git" ]]; then
git -C "$xmlloc" checkout $git_branch
git -C "$xmlloc" pull
else
if [[ -z $archive ]]; then
echo
read -r -p "Do you wish to archive existing xml files? (Y/N) : " archive
if [[ "$archive" = "y" ]] || [[ "$archive" = "Y" ]];
then
archive="YES"
else
archive="NO"
fi
fi
fi
fi
# Check for existing items, archiving if necessary.
for (( loop=0; loop<${#readfiles[@]}; loop++ ))
do
if [[ "$archive" == "YES" ]]; then
if [[ $(ls -1 "$xmlloc"/"${readfiles[$loop]}"/* 2>/dev/null | wc -l) -gt 0 ]]; then
echo
echo " [check_xml_folder] Archiving API endpoint: ${readfiles[$loop]}"
ditto -ck "$xmlloc/${readfiles[$loop]}" "$xmlloc/archives/${readfiles[$loop]}-$( date +%Y%m%d%H%M%S ).zip"
rm -rf "${xmlloc:?}/${readfiles[$loop]}"
fi
fi
# Check and create the JSS xml resource folders if missing.
if [[ ! -f "$xmlloc/${readfiles[$loop]}" ]]; then
mkdir -p "$xmlloc/${readfiles[$loop]}/id_list"
mkdir -p "$xmlloc/${readfiles[$loop]}/fetched_xml"
mkdir -p "$xmlloc/${readfiles[$loop]}/parsed_xml"
fi
done
}
setup_the_action() {
# Set default instance list
default_instance_list="prd"
# Check and create the JSS xml folder and archive folders if missing.
xml_folder="$xml_folder_default"
mkdir -p "${xml_folder}"
formatted_list="${xml_folder}/formatted_list.xml"
# ensure nothing carried over
do_all_instances=""
# select the instance that the action will be performed on
if [[ $source_instance_list ]]; then
instance_list_file="$source_instance_list"
fi
choose_source_instance
source_instance_list="$instance_list_file"
default_instance_list="$source_instance_list" # reset default to match source
# if [[ "$action_type" != "download" ]]; then
# # now select the destination instances
# if [[ $dest_instance_list ]]; then
# instance_list_file="$dest_instance_list"
# else
# instance_list_file=""
# fi
# choose_destination_instances
# fi
}
grab_existing_jss_xml() {
# determine source jss_url
jss_url="$source_instance"
# Print our settings
echo
echo " [grab_existing_jss_xml] Server: $jss_url"
echo
if [[ -d "$xmlloc/.git" ]]; then
git -C "$xmlloc" checkout $git_branch
git -C "$xmlloc" pull
fi
# Loop around the array of JSS endpoints we set up earlier.
for (( loop=0; loop<${#readfiles[@]}; loop++ ))
do
# Set our result incremental variable to 1
resultInt=1
# Work out where things are going to be stored on this loop
# instance_loc=$(sed 's|https://||' <<< "$jss_url" | sed 's|/|_|g' | sed 's|\.|_|g')
formattedList="$xmlloc/${readfiles[$loop]}/id_list/formattedList.xml"
plainList="$xmlloc/${readfiles[$loop]}/id_list/plainList.txt"
plainListAccountsUsers="$xmlloc/${readfiles[$loop]}/id_list/plainListAccountsUsers.txt"
plainListAccountsGroups="$xmlloc/${readfiles[$loop]}/id_list/plainListAccountsGroups.txt"
fetchedResult="$xmlloc/${readfiles[$loop]}/fetched_xml/result-$resultInt.xml"
fetchedResultAccountsUsers="$xmlloc/${readfiles[$loop]}/fetched_xml/userResult-$resultInt.xml"
fetchedResultAccountsGroups="$xmlloc/${readfiles[$loop]}/fetched_xml/groupResult-$resultInt.xml"
# ensure all the directories exist
mkdir -p "$xmlloc/${readfiles[$loop]}/id_list" "$xmlloc/${readfiles[$loop]}/fetched_xml"
# Grab all existing ID's for the current API endpoint we're processing
echo
echo " [grab_existing_jss_xml] Creating ID list for ${readfiles[$loop]} on ${jss_url}"
# echo "using $jss_url/JSSResource/${readfiles[$loop]} with user $origjssapiuser:$origjssapipwd"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$jss_url/JSSResource/${readfiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# cat "$curl_output_file" ## TEMP
# echo ## TEMP
# echo "$formattedList" ## TEMP
# format the output into a file
xmllint --format "$curl_output_file" 2>/dev/null > "$formattedList"
if [[ ${readfiles[$loop]} == "accounts" ]]; then
# Accounts have to be treated differently
if [[ $(grep -c "<users/>" "$formattedList") == "0" ]]; then
echo
echo " [grab_existing_jss_xml] Creating plain list of user ID's..."
sed '/<site>/,/<\/site>/d' "$formattedList" | sed '/<groups>/,/<\/groups>/d' | awk -F '<id>|</id>' '/<id>/ {print $2}' > "$plainListAccountsUsers"
else
rm "$formattedList"
fi
if [[ $(grep -c "<groups/>" "$formattedList") == "0" ]]; then
echo
echo " [grab_existing_jss_xml] Creating plain list of group ID's..."
sed '/<site>/,/<\/site>/d' "$formattedList" | sed '/<users>/,/<\/users>/d' | awk -F '<id>|</id>' '/<id>/ {print $2}' > "$plainListAccountsGroups"
else
rm "$formattedList"
fi
elif [[ ${readfiles[$loop]} == "smtpserver" || ${readfiles[$loop]} == "activationcode" || ${readfiles[$loop]} == "computerinventorycollection" ]]; then
echo
echo " [grab_existing_jss_xml] Parsing ${readfiles[$loop]}"
cat "$formattedList" > "$xmlloc/${readfiles[$loop]}/parsed_xml/parsed_result1.xml"
else
if [[ $(grep -c "<size>0" "$formattedList") == "0" ]]; then
echo
echo " [grab_existing_jss_xml] Creating a plain list of ${readfiles[$loop]} ID's "
awk -F'<id>|</id>' '/<id>/ {print $2}' "$formattedList" > "$plainList"
else
rm "$formattedList"
fi
fi
# Work out how many IDs are present IF formattedlist is present. Grab and download each one for the specific search we're doing. Special code for accounts because the API is annoyingly different from the rest.
files=("$xmlloc/${readfiles[$loop]}"/id_list/*)
if [[ ${#files[@]} -gt 0 ]]; then
# echo "${readfiles[$loop]]}" ## TEMP
case "${readfiles[$loop]}" in
accounts)
totalFetchedIDsUsers=$( wc -l < "$plainListAccountsUsers" | sed -e 's/^[ \t]*//' )
while IFS= read -r userID; do
echo
echo " [grab_existing_jss_xml] Downloading User ID number $userID ($resultInt/$totalFetchedIDsUsers)"
# determine source jss_url
jss_url="$source_instance"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$jss_url/JSSResource/${readfiles[$loop]}/userid/$userID"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# format the output
fetchedResultAccountsUsers=$(xmllint --format "$curl_output_file" 2>/dev/null)
# itemID=$( echo "$fetchedResultAccountsUsers" | grep "<id>" | awk -F '<id>|</id>' '{ print $2; exit; }')
# itemName=$( echo "$fetchedResultAccountsUsers" | grep "<name>" | awk -F '<name>|</name>' '{ print $2; exit; }')
# cleanedName=$( echo "$itemName" | sed 's/[:\/\\]//g' )
echo "$fetchedResultAccountsUsers" > "$xmlloc/${readfiles[$loop]}/fetched_xml/user_$resultInt.xml"
resultInt=$((resultInt + 1))
done < "$plainListAccountsUsers"
resultInt=1
totalFetchedIDsGroups=$( wc -l < "$plainListAccountsGroups" | sed -e 's/^[ \t]*//' )
while IFS= read -r groupID; do
echo
echo " [grab_existing_jss_xml] Downloading Group ID number $groupID ($resultInt/$totalFetchedIDsGroups)"
# determine source jss_url
jss_url="$source_instance"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$jss_url/JSSResource/${readfiles[$loop]}/groupid/$groupID"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# format the output
fetchedResultAccountsGroups=$(xmllint --format "$curl_output_file" 2>/dev/null)
# itemID=$( echo "$fetchedResultAccountsGroups" | grep "<id>" | awk -F '<id>|</id>' '{ print $2; exit; }')
# itemName=$( echo "$fetchedResultAccountsGroups" | grep "<name>" | awk -F '<name>|</name>' '{ print $2; exit; }')
# cleanedName=$( echo "$itemName" | sed 's/[:\/\\]//g' )
echo "$fetchedResultAccountsGroups" > "$xmlloc/${readfiles[$loop]}/fetched_xml/group_$resultInt.xml"
resultInt=$((resultInt + 1))
done < "$plainListAccountsGroups"
;;
smtpserver|activationcode)
echo
echo " [grab_existing_jss_xml] No additional downloading required for: ${readfiles[$loop]}"
;;
*)
if [[ -f "$plainList" ]]; then
totalFetchedIDs=$(wc -l < "$plainList" | sed -e 's/^[ \t]*//')
while IFS= read -r apiID; do
echo
echo " [grab_existing_jss_xml] Downloading ID number $apiID ($resultInt/$totalFetchedIDs)"
# determine source jss_url
jss_url="$source_instance"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$jss_url/JSSResource/${readfiles[$loop]}/id/$apiID"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# format the output into a file
xmllint --format "$curl_output_file" 2>/dev/null > "$fetchedResult"
resultInt=$((resultInt + 1))
fetchedResult="$xmlloc/${readfiles[$loop]}/fetched_xml/result-$resultInt.xml"
done < "$plainList"
else
echo
echo " [grab_existing_jss_xml] No "${readfiles[$loop]}" items found"
fi
;;
esac
# Depending which API endpoint we're dealing with, parse the grabbed files into something we can upload later.
case "${readfiles[$loop]}" in
computergroups|mobiledevicegroups)
echo
echo " [grab_existing_jss_xml] Parsing ${readfiles[$loop]}"
for fetched_file in "$xmlloc/${readfiles[$loop]}"/fetched_xml/*; do
[[ -f "$fetched_file" ]] || break # handle the case of no matching files
echo " [grab_existing_jss_xml] Parsing group: $fetched_file"
if grep -q "<is_smart>false</is_smart>" "$fetched_file"; then
if [[ "${readfiles[$loop]}" == "computergroups" ]]; then
echo " [grab_existing_jss_xml] $fetched_file is a static computer group"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/static_group_parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<computers>/,/<\/computers/d' > "$parsed_file"
elif [[ "${readfiles[$loop]}" == "mobiledevicegroups" ]]; then
echo " [grab_existing_jss_xml] $fetched_file is a static mobile device group"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/static_group_parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<mobile_devices>/,/<\/mobile_devices/d' > "$parsed_file"
fi
else
if [[ "${readfiles[$loop]}" == "computergroups" ]]; then
echo " [grab_existing_jss_xml] $fetched_file is a smart computer group..."
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/smart_group_parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<computers>/,/<\/computers/d' > "$parsed_file"
elif [[ "${readfiles[$loop]}" == "mobiledevicegroups" ]]; then
echo " [grab_existing_jss_xml] $fetched_file is a smart mobile device group..."
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/smart_group_parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<mobile_devices>/,/<\/mobile_devices/d' > "$parsed_file"
fi
fi
done
;;
policies)
echo
echo " [grab_existing_jss_xml] Parsing ${readfiles[$loop]}"
for fetched_file in "$xmlloc/${readfiles[$loop]}"/fetched_xml/*; do
[[ -f "$fetched_file" ]] || break # handle the case of no matching files
echo
echo " [grab_existing_jss_xml] Parsing policy: $fetched_file"
if grep -q "<name>No category assigned</name>" "$fetched_file"; then
echo " [grab_existing_jss_xml] Policy $fetched_file is not assigned to a category. Ignoring."
else
echo " [grab_existing_jss_xml] Processing policy file $fetched_file"
# download the icon before we delete it from the xml!
fetch_icon "$fetched_file"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<self_service_icon>/,/<\/self_service_icon>/d' | sed '/<computers>/,/<\/computers>/d' | sed '/<limit_to_users>/,/<\/limit_to_users>/d' | sed '/<users>/,/<\/users>/d' | sed '/<user_groups>/,/<\/user_groups>/d' > "$parsed_file"
fi
done
;;
restrictedsoftware)
echo
echo " [grab_existing_jss_xml] Parsing ${readfiles[$loop]}"
for fetched_file in "$xmlloc/${readfiles[$loop]}"/fetched_xml/*; do
[[ -f "$fetched_file" ]] || break # handle the case of no matching files
echo
echo " [grab_existing_jss_xml] Parsing item: $fetched_file"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed '/<computers>/,/<\/computers>/d' | sed '/<limit_to_users>/,/<\/limit_to_users>/d' | sed '/<users>/,/<\/users>/d' | sed '/<user_groups>/,/<\/user_groups>/d' > "$parsed_file"
done
;;
ldapservers|distributionpoints)
echo
echo " [grab_existing_jss_xml] Parsing: ${readfiles[$loop]}."
for fetched_file in "$xmlloc/${readfiles[$loop]}"/fetched_xml/*; do
[[ -f "$fetched_file" ]] || break # handle the case of no matching files
echo
echo " [grab_existing_jss_xml] Parsing $fetched_file"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" | sed -e "s|<password_sha256.*|<password>${ldap_password}</password>|" | sed -e "s|<ssh_password_sha256.*|<ssh_password>${smb_pass}</ssh_password>|" | sed -e "s|<read_only_password_sha256.*|<read_only_password>${smb_readonly_pass}</read_only_password>|" | sed -e "s|<read_write_password_sha256.*|<read_write_password>${smb_pass}</read_write_password>|" | sed -e "s|<http_password_sha256.*|<http_password>${smb_readonly_pass}</http_password>|" > "$parsed_file"
done
;;
smtpserver|activationcode)
echo
echo " [grab_existing_jss_xml] No special parsing needed for: ${readfiles[$loop]}."
;;
*)
echo
echo " [grab_existing_jss_xml] No special parsing needed for: ${readfiles[$loop]}."
for fetched_file in "$xmlloc/${readfiles[$loop]}"/fetched_xml/*; do
[[ -f "$fetched_file" ]] || break # handle the case of no matching files
echo
echo " [grab_existing_jss_xml] Parsing $fetched_file"
parsed_file="$xmlloc/${readfiles[$loop]}/parsed_xml/parsed_"$(basename "$fetched_file")
grep -v '<id>' "$fetched_file" > "$parsed_file"
done
;;
esac
else
echo
echo " [grab_existing_jss_xml] Resource ${readfiles[$loop]} empty. Skipping."
fi
done
# write to git
if [[ -d "$xmlloc/.git" ]];
then
git -C "$xmlloc" add --all
git_date=$(date)
git -C "$xmlloc" commit -m "Updated at $git_date"
git -C "$xmlloc" push
fi
}
fetch_icon() {
local fetchedFile="$1"
# get icon details from fetched xml
icon_filename=$( xmllint --xpath '//self_service/self_service_icon/filename/text()' "${fetchedFile}" 2>/dev/null )
# if [[ "$icon_filename" ]]; then
# echo " [fetch_icon] Icon name found: $icon_filename"
# fi
icon_url=$( xmllint --xpath '//self_service/self_service_icon/uri/text()' "${fetchedFile}" 2>/dev/null )
# if [[ "$icon_url" ]]; then
# echo " [fetch_icon] Icon URL found: $icon_url"
# fi
# download icon to local folder
if [[ $icon_filename && $icon_url ]]; then
echo " [fetch_icon] Downloading $icon_filename from $icon_url"
# determine source jss_url
jss_url="$source_instance"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$icon_url"
send_curl_request
# copy icon to final icon location
cp "$curl_output_file" "$icons_folder/$icon_filename"
else
echo " [fetch_icon] No icon in this policy"
fi
}
wipe_jss() {
# THIS IS YOUR LAST CHANCE TO PUSH THE CANCELLATION BUTTON
# determine source jss_url
jss_url="$source_instance"
echo
echo "This action will erase items on $jss_url."
echo "Are you utterly sure you want to do this?"
read -r -p "(Default is NO. Type YES to go ahead) : " arewesure
# Check for the skip
if [[ $arewesure != "YES" ]]; then
echo
echo "Ok, skipping the wipe."
return
fi
# OK DO IT
for (( loop=0; loop<${#wipefiles[@]}; loop++ )); do
if [[ "${wipefiles[$loop]}" == "accounts" ]]; then
echo
echo " [wipe_jss] Skipping ${wipefiles[$loop]} API endpoint. Or we can't get back in!"
elif [[ ${wipefiles[$loop]} == "smtpserver" || ${wipefiles[$loop]} == "activationcode" || ${wipefiles[$loop]} == "computerinventorycollection" ]]; then
echo
echo " [wipe_jss] Skipping ${wipefiles[$loop]} API endpoint as no delete option is available via API."
else
# Set our result incremental variable to 1
resultInt=1
# Grab all existing ID's for the current API endpoint we're processing
echo
echo " [wipe_jss] Processing ID list for ${wipefiles[$loop]}"
echo " [wipe_jss] Dest: $jss_url"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# send request
curl_url="$jss_url/JSSResource/${wipefiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# format the output into a file
xmllint --format "$curl_output_file" 2>/dev/null > "${xmlloc}/unprocessedid.txt"
# Check if any ids have been captured. Skip if none present.
check=$(grep -c "<size>0</size>" "${xmlloc}/unprocessedid.txt")
if [[ "$check" == "0" ]]; then
# What are we deleting?
echo
echo " [wipe_jss] Deleting ${wipefiles[$loop]}"
# Process all the item id numbers
awk -F '<id>|</id>' '/<id>/ {print $2}' "${xmlloc}/unprocessedid.txt" > "${xmlloc}/processedid.txt"
# Delete all the item id numbers
totalFetchedIDs=$( wc -l < "${xmlloc}/processedid.txt" | sed -e 's/^[ \t]*//' )
while read -r line; do
echo
echo " [wipe_jss] Deleting ID number $line ($resultInt/$totalFetchedIDs)"
# send request
curl_url="$jss_url/JSSResource/${wipefiles[$loop]}/id/$line"
curl_args=("--request")
curl_args+=("DELETE")
curl_args+=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
resultInt=$((resultInt + 1))
done < "${xmlloc}/processedid.txt"
else
echo
echo " [wipe_jss] API endpoint ${wipefiles[$loop]} is empty. Skipping."
fi
fi
done
}
put_on_new_jss() {
echo
echo " [put_on_new_jss] Writing to $jss_url"
for (( loop=0; loop<${#writefiles[@]}; loop++ )); do
if [[ $(ls -1 "$xmlloc/${writefiles[$loop]}/parsed_xml"/* 2>/dev/null | wc -l) -gt 0 ]]; then
# Set our result incremental variable to 1
resultInt=1
echo
echo
echo " [put_on_new_jss] Posting ${writefiles[$loop]} to JSS instance: $jss_url"
# determine source jss_url
jss_url="$source_instance"
# check for an existing token, get a new one if required
set_credentials "$jss_url"
# get XML object name from object type (URL style)
api_xml_object=$(get_api_object_from_type "${writefiles[$loop]}")
api_xml_object_plural=$(get_plural_from_api_xml_object "$api_xml_object")
case "${writefiles[$loop]}" in
accounts)
echo
echo " [put_on_new_jss] Posting user accounts."
totalParsedResourceXML_user=$( ls $xmlloc/${writefiles[$loop]}/parsed_xml/*user* | wc -l | sed -e 's/^[ \t]*//' )
postInt_user=0
for xmlPost_user in "$xmlloc/${writefiles[$loop]}/parsed_xml"/*user*; do
(( postInt_user++ ))
echo
echo
echo " [put_on_new_jss] Posting User Account $postInt_user/$totalParsedResourceXML_user '$xmlPost_user' from $(basename "$xmlPost_user")"
# send request
curl_url="$jss_url/JSSResource/accounts/userid/0"
curl_args=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$xmlPost_user")
send_curl_request
done
echo
echo " [put_on_new_jss] Posting user group accounts."
totalParsedResourceXML_group=$( ls "$xmlloc/${writefiles[$loop]}/parsed_xml"/*group* | wc -l | sed -e 's/^[ \t]*//' )
postInt_group=0
for xmlPost_group in "$xmlloc/${writefiles[$loop]}/parsed_xml/"*group*; do
(( postInt_group++ ))
echo
echo
echo " [put_on_new_jss] Posting User Group $postInt_group/$totalParsedResourceXML_group '$xmlPost_group' from $(basename "$xmlPost_group")"
# send request
curl_url="$jss_url/JSSResource/accounts/groupid/0"
curl_args=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$xmlPost_group")
send_curl_request
done
;;
computergroups|mobiledevicegroups)
echo
echo " [put_on_new_jss] Posting static groups."
# grab a list of existing groups
curl_url="$jss_url/JSSResource/${writefiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# save the output file
instance_check_file="$xmlloc/${writefiles[$loop]}/$(basename "$jss_url").output.txt"
xmllint --format "$curl_output_file" 2>/dev/null > "$instance_check_file"
totalParsedResourceXML_staticGroups=$( ls "$xmlloc/${writefiles[$loop]}/parsed_xml/"static_group_parsed* | wc -l | sed -e 's/^[ \t]*//' )
postInt_static=0
for parsedXML_static in "$xmlloc/${writefiles[$loop]}/parsed_xml"/static_group_parsed*; do
(( postInt_static++ ))
# look for existing group and update it rather than create a new one if it exists
source_name="$( grep "<name>" < "$parsedXML_static" | head -n 1 | awk -F '<name>|</name>' '{ print $2; exit; }' | sed -e 's|&|\&|g' )"
# source_name_urlencode="$( echo "$source_name" | sed -e 's| |%20|g' | sed -e 's|&|%26|g' )"
echo
echo " [put_on_new_jss] Posting Static Group $postInt_static/$totalParsedResourceXML_staticGroups '$source_name' from $(basename "$parsedXML_static")"
# get id from output
existing_id=$(xmllint --xpath "//${api_xml_object_plural}/${api_xml_object}[name = \"$source_name\"]/id/text()" "$instance_check_file" 2>/dev/null)
if [[ $existing_id ]]; then
echo
echo " [put_on_new_jss] Static group '$source_name' already exists - not overwriting..."
else
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/0"
curl_args=("--request")
curl_args+=("POST")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML_static")
send_curl_request
# slow down to allow Jamf Pro 10.39+ to to its thing
sleep 2
fi
done
echo
echo " [put_on_new_jss] Posting smart groups"
totalParsedResourceXML_smartGroups=$(ls $xmlloc/${writefiles[$loop]}/parsed_xml/smart_group_parsed* | wc -l | sed -e 's/^[ \t]*//')
postInt_smart=0
for parsedXML_smart in "$xmlloc/${writefiles[$loop]}/parsed_xml"/smart_group_parsed*; do
(( postInt_smart++ ))
# look for existing entry and update it rather than create a new one if it exists
source_name="$( grep "<name>" < "$parsedXML_smart" | head -n 1 | awk -F '<name>|</name>' '{ print $2; exit; }' | sed -e 's|&|\&|g' )"
echo
echo " [put_on_new_jss] Posting Smart Group $postInt_smart/$totalParsedResourceXML_smartGroups '$source_name' from $(basename "$parsedXML_smart")"
# get id from output
existing_id=$(xmllint --xpath "//${api_xml_object_plural}/${api_xml_object}[name = \"$source_name\"]/id/text()" "$instance_check_file" 2>/dev/null)
if [[ $existing_id ]]; then
if [[ $overwrite_items == "yes" ]]; then
# We only want to replace certain smart groups, namely those with "test version installed" or "current version installed" in their name.
# if [[ "$source_name" == *"version installed"* ]]; then # TEMP DISABLE
echo
echo " [put_on_new_jss] Smart Group '$source_name' can be replaced"
echo
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/$existing_id"
curl_args=("--request")
curl_args+=("PUT")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML_smart")
send_curl_request
# slow down to allow Jamf Pro 10.39+ to to its thing
sleep 2
else
echo " [put_on_new_jss] Smart Group '$source_name' already exists... skipping"
fi
else
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/0"
curl_args=("--request")
curl_args+=("POST")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML_smart")
send_curl_request
# slow down to allow Jamf Pro 10.39+ to to its thing
sleep 2
fi
done
;;
smtpserver|activationcode|computerinventorycollection)
echo
echo " [put_on_new_jss] Posting $parsedXML ($postInt/$totalParsedResourceXML)"
for parsedXML in "$xmlloc/${writefiles[$loop]}/parsed_xml/"*.xml; do
# look for name
source_name="$( grep "<name>" < "$parsedXML" | head -n 1 | awk -F '<name>|</name>' '{ print $2; exit; }' | sed -e 's|&|\&|g' )"
echo
echo " [put_on_new_jss] Posting ${writefiles[$loop]} $postInt/$totalParsedResourceXML '$source_name' from $(basename "$parsedXML")"
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}"
curl_args=("--request")
curl_args+=("PUT")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML")
send_curl_request
done
;;
policies)
totalParsedResourceXML=$(ls $xmlloc/${writefiles[$loop]}/parsed_xml | wc -l | sed -e 's/^[ \t]*//')
postInt=0
# grab a list of existing policies
curl_url="$jss_url/JSSResource/${writefiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# save the output file
instance_check_file="$xmlloc/${writefiles[$loop]}/$(basename "$jss_url").output.txt"
xmllint --format "$curl_output_file" 2>/dev/null > "$instance_check_file"
for parsedXML in "$xmlloc/${writefiles[$loop]}/parsed_xml/"*.xml; do
(( postInt++ ))
# look for existing policy and update it rather than create a new one if it exists
# Re-add icon from local source - first get the policy name from the parsed XML
source_name="$( xmllint --xpath //general/name "$parsedXML" | awk -F '<name>|</name>' '{ print $2; exit; }' | sed -e 's|&|\&|g' )"
# source_name_urlencode="$( echo "$source_name" | sed -e 's| |%20|g' | sed -e 's|&|%26|g' )"
echo
echo " [put_on_new_jss] Posting ${writefiles[$loop]} $postInt/$totalParsedResourceXML '$source_name' from $(basename "$parsedXML")"
# get id from output
existing_id=$(xmllint --xpath "//${api_xml_object_plural}/${api_xml_object}[name = \"$source_name\"]/id/text()" "$instance_check_file" 2>/dev/null)
if [[ $existing_id ]]; then
if [[ $overwrite_items == "yes" ]]; then
echo " [put_on_new_jss] ${writefiles[$loop]} '$source_name' already exists (ID=$existing_id)... overwriting"
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/$existing_id"
curl_args=("--request")
curl_args+=("PUT")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML")
send_curl_request
else
echo " [put_on_new_jss] policy '$source_name' already exists... skipping"
fi
else
# existing policy not found, creating new one
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/0"
curl_args=("--request")
curl_args+=("POST")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML")
send_curl_request
fi
# Re-add icon from local source - first get the icon name from the source policy (if there is one, otherwise skip)
# Since we already extracted the icon in the parsed file, we need to use the original fetched file for this. The numbers of the fetched and parsed files are the same so we can use this to get the correct fetched file.
parsingNumber=$( basename "${parsedXML}" | sed -e 's/^[^-]*-//' | sed -e 's/\.xml//' )
fetchedXMLFile="${xmlloc}/${writefiles[$loop]}/fetched_xml/result-${parsingNumber}.xml"
icon_name="$( xmllint --xpath //self_service/self_service_icon/filename "$fetchedXMLFile" | awk -F '<filename>|</filename>' '{ print $2; exit; }' )"
if [[ $icon_name ]]; then
echo
echo " [put_on_new_jss] Icon name: $icon_name"
# If an icon exists in our repo that doesn't match the icon in an existing policy, upload it.
# Method thanks to https://list.jamfsoftware.com/jamf-nation/discussions/23231/mass-icon-upload
if [[ -f "$icons_folder/$icon_name" ]]; then
echo
echo " [put_on_new_jss] Matching icon found: $icons_folder/$icon_name"
# To upload the file we need to know the policy number that was just created.
# To do this we submit a request based on the policy name
policy_name="$( xmllint --xpath //general/name "$parsedXML" | awk -F '<name>|</name>' '{ print $2; exit; }' )"
policy_name_urlencode="$( echo "$policy_name" | sed -e 's| |%20|g' | sed -e 's|&|%26|g' )"
echo
echo " [put_on_new_jss] URL: $jss_url/JSSResource/policies/name/$policy_name_urlencode"
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# get id from output
policy_id=$(xmllint --xpath "//${api_xml_object_plural}/${api_xml_object}[name = \"$policy_name\"]/id/text()" "$curl_output_file" 2>/dev/null)
echo
echo " [put_on_new_jss] Policy number $policy_id identified..."
# Let's see if there is already an icon with the correct name.
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/$policy_id"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
existing_self_service_icon=$(xmllint --xpath //self_service/self_service_icon/filename "$curl_output_file" 2>/dev/null | awk -F '<filename>|</filename>' '{ print $2; exit; }' )
if [[ "$existing_self_service_icon" != "$icon_name" ]]; then
echo
echo " [put_on_new_jss] Existing icon does not match local repo (or is absent). Uploading $icon_name"
# Now upload the file to the correct policy_id
# send request
curl_url="$jss_url/JSSResource/fileuploads/policies/id/$policy_id"
curl_args=("-F")
curl_args+=(name=@"$icons_folder/$icon_name")
send_curl_request
# Now check if the icon is there
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/$policy_id"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# save the output file
icon_check_file="$xmlloc/${writefiles[$loop]}/$(basename "$jss_url").iconcheck.txt"
cp "$curl_output_file" "$icon_check_file"
self_service_icon_uri=$( xmllint --xpath //self_service/self_service_icon/uri "$icon_check_file" 2>/dev/null | awk -F '<uri>|</uri>' '{ print $2; exit; }' )
if [[ "$self_service_icon_uri" ]]; then
echo
echo " [put_on_new_jss] Icon successfully uploaded to $self_service_icon_uri"
else
echo
echo " [put_on_new_jss] $icon_name errored when attempting to upload it. Continuing anyway..."
fi
else
echo
echo " [put_on_new_jss] Existing icon matches repo. No need to re-upload."
fi
else
echo
echo " [put_on_new_jss] Icon $icons_folder/$icon_name not found. Continuing..."
fi
else
echo
echo " [put_on_new_jss] No icon in source policy. Continuing..."
fi
done
;;
*)
totalParsedResourceXML=$(ls $xmlloc/${writefiles[$loop]}/parsed_xml | wc -l | sed -e 's/^[ \t]*//')
postInt=0
# get list of existing items
curl_url="$jss_url/JSSResource/${writefiles[$loop]}"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# save the output file
instance_check_file="$xmlloc/${writefiles[$loop]}/$(basename "$jss_url").output.txt"
xmllint --format "$curl_output_file" 2>/dev/null > "$instance_check_file"
for parsedXML in "$xmlloc/${writefiles[$loop]}/parsed_xml/"*.xml; do
(( postInt++ ))
# look for existing entry and update it rather than create a new one if it exists
source_name="$( grep "<name>" < "$parsedXML" | head -n 1 | awk -F '<name>|</name>' '{ print $2; exit; }' | sed -e 's|&|\&|g' )"
echo
echo " [put_on_new_jss] Posting ${writefiles[$loop]} $postInt/$totalParsedResourceXML '$source_name' from $(basename "$parsedXML")"
# get id from output
existing_id=$(xmllint --xpath "//${api_xml_object_plural}/${api_xml_object}[name = \"$source_name\"]/id/text()" "$instance_check_file" 2>/dev/null)
# TEMP
# echo "existing_id ID: $existing_id"
# echo "api_xml_object_plural: $api_xml_object_plural"
# echo "api_xml_object: $api_xml_object"
# echo "source_name: $source_name"
# echo "instance_check_file: $instance_check_file"
if [[ $existing_id ]]; then
if [[ $overwrite_items == "yes" && "${writefiles[$loop]}" != "categories" ]]; then
echo " [put_on_new_jss] ${writefiles[$loop]} '$source_name' already exists (ID=$existing_id)... overwriting"
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/$existing_id"
curl_args=("--request")
curl_args+=("PUT")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML")
send_curl_request
else
echo " [put_on_new_jss] ${writefiles[$loop]} '$source_name' already exists... skipping"
fi
else
# existing item not found, creating new one
# send request
curl_url="$jss_url/JSSResource/${writefiles[$loop]}/id/0"
curl_args=("--request")
curl_args+=("POST")
curl_args+=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data-binary")
curl_args+=(@"$parsedXML")
send_curl_request
fi
done
;;
esac
else
echo
echo " [put_on_new_jss] Resource ${writefiles[$loop]} empty. Skipping."
fi
done
# Setting IFS back to default
IFS=$OIFS
}
main_menu() {
# -------------------------------------------------------------------------
# Configure Logging
# -------------------------------------------------------------------------
# Logging
log_file="$HOME/Library/Logs/JAMF/jamf-migration-tool.log"
if [[ ! -f "$log_file" ]]; then
mkdir -p "$( dirname "$log_file" )"
touch "$log_file"
fi
exec &> >( tee -a "$log_file" >&2 )
# Create icons folder
mkdir -p "$icons_folder"
# -------------------------------------------------------------------------
# Set the source and destination server(s) and instance(s)
# -------------------------------------------------------------------------
# Set default server
default_source_server="prd"
# These are the endpoints we're going to read
readfiles=()
while read -r line; do