-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_CSV_to_Group.sh
109 lines (86 loc) · 2.65 KB
/
Add_CSV_to_Group.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
#!/bin/bash
#Matthew Prins 2023
#https://github.com/MatthewPrins/Jamf/
#Add to static group based on CSV file
#-------------------
#Editable variables
#Jamf credentials
username="xxxxxx"
password="xxxxxx"
url="https://xxxxxx.jamfcloud.com"
#path of CSV file
#CSV file should have one column of serials
CSVfile="xxxxxx.csv"
#type of smart group: $typegroup can be "computer" or "device"
typegroup="device"
#number of mobile device static group -- get from URL
staticgroup="1234"
#-------------------
#Token function -- from https://developer.jamf.com/jamf-pro/docs/jamf-pro-api-overview
getBearerToken() {
response=$(curl -s -u "$username":"$password" "$url"/api/v1/auth/token -X POST)
bearerToken=$(echo "$response" | plutil -extract token raw -)
tokenExpiration=$(echo "$response" | plutil -extract expires raw - | awk -F . '{print $1}')
tokenExpirationEpoch=$(date -j -f "%Y-%m-%dT%T" "$tokenExpiration" +"%s")
}
#get path for computer or devices
if [[ $typegroup = "computer" ]]; then
grouptype="computergroups"
pathtype="computer"
elif [[ $typegroup = "device" ]]; then
grouptype="mobiledevicegroups"
pathtype="mobile_device"
fi
#input CSV to two arrays
while IFS=',' read -ra array; do
serialarray+=("${array[0]}")
done < $CSVfile
echo "${#serialarray[@]} lines in CSV file"
#get token
getBearerToken
#get total count of devices
numberDevices=${#serialarray[@]}
echo
echo 0 / $numberDevices
#iterate over serials in CSV
counter=0
groupaddlist=""
for value in ${!serialarray[@]}
do
#tr bit to get rid of special characters
serial=$(echo ${serialarray[$value]} | tr -d '[:cntrl:]')
id=$(curl -s -H "Authorization: Bearer ${bearerToken}" "Accept: application/xml" \
$url/JSSResource/mobiledevices/serialnumber/$serial/subset/General \
| xmllint --xpath "//mobile_device/general/id" - 2> /dev/null \
| sed 's/<id>//g' \
| sed 's/<\/id>/ /g' \
| sed 's/.$//')
#if serial found then add
if [[ $id != "" ]]
then
groupaddlist="$groupaddlist<$pathtype><id>$id</id></$pathtype>"
fi
#print status every 10
let "counter+=1"
if [[ $(expr $counter % 10) = "0" ]]
then
echo $counter / $numberDevices
fi
#reset token every 1000
if [[ $(expr $counter % 1000) = "0" ]]
then
getBearerToken
fi
done
echo $numberDevices / $numberDevices
echo updating...
echo $groupaddlist
#send PUT command to Jamf to update group membership
curl --silent \
--request PUT \
--url $url/JSSResource/$grouptype/id/$staticgroup \
--header "Authorization: Bearer $bearerToken" \
--header 'Accept: application/xml' \
--header 'Content-Type: application/xml' \
--data "<"$pathtype"_group><"$pathtype"_additions>$groupaddlist</"$pathtype"_additions></"$pathtype"_group>"
echo "done"