-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_email_list_from_computer_group.sh
81 lines (70 loc) · 2.52 KB
/
generate_email_list_from_computer_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
#!/bin/bash
#This script is a template to grab the email addresses for users in a particular Computer Group within Jamf and create a CSV from them
# Define Variables
jamfProURL=""
client_id=""
client_secret=""
#Specify the Computer Group Number
ID=""
#Specify the output file for the CSV
outputFile="/path/to/output.csv"
getAccessToken() {
response=$(curl --silent --location --request POST "${jamfProURL}/api/oauth/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=${client_id}" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=${client_secret}")
access_token=$(echo "$response" | plutil -extract access_token raw -)
token_expires_in=$(echo "$response" | plutil -extract expires_in raw -)
token_expiration_epoch=$(($current_epoch + $token_expires_in - 1))
}
checkTokenExpiration() {
current_epoch=$(date +%s)
if [[ token_expiration_epoch -ge current_epoch ]]
then
echo "Token valid until the following epoch time: " "$token_expiration_epoch"
else
echo "No valid token available, getting new token"
getAccessToken
fi
}
invalidateToken() {
responseCode=$(curl -w "%{http_code}" -H "Authorization: Bearer ${access_token}" $jamfProURL/api/v1/auth/invalidate-token -X POST -s -o /dev/null)
if [[ ${responseCode} == 204 ]]
then
echo "Token successfully invalidated"
access_token=""
token_expiration_epoch="0"
elif [[ ${responseCode} == 401 ]]
then
echo "Token already invalid"
else
echo "An unknown error occurred invalidating the token"
fi
}
#generate a Bearer Token and set it as a variable
checkTokenExpiration
#Obtain computer IDs for computers in Jamf Pro from the Smart Group
computerIDs=$( /usr/bin/curl -s \
--header "authorization: Bearer $access_token" \
--header "Accept: text/xml" \
--request GET \
$jamfProURL/computergroups/id/$ID | xmllint --xpath '/computer_group/computers' - | xmllint --format - | awk -F '[<>]' '/<id>/{print $3}' )
for computerID in $computerIDs;do
specificID=$(/usr/bin/curl -s \
--header "authorization: Bearer $access_token" \
--header "Accept: text/xml" \
--request GET \
$jamfProURL/computers/id/$computerID )
userEmail=$( echo $specificID | xmllint --xpath '/computer/location/email_address/text()' - )
if [[ ! -e $outputFile ]];then
touch $outputFile
fi
#Check the output file to see if email is already listed, if so, skip it
DuplicateCheck=$( cat $outputFile | grep "$userEmail" )
if [[ -z $DuplicateCheck ]];then
echo $userEmail", " >> $outputFile
fi
done
#Invalidate Token
invalidateToken