-
Notifications
You must be signed in to change notification settings - Fork 52
/
ExchangeDomainSharedContacts.py
executable file
·119 lines (110 loc) · 6.6 KB
/
ExchangeDomainSharedContacts.py
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
#!/usr/bin/env python3
"""
# Purpose: Make all users in DomainB be domain shared contacts in DomainA and all users in DomainA be domain shared contacts in DomainB
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# Make DomainB users be domain shared contacts in DomainA
# 1: Generate the two data files, DomainB users and DomainA domain shared contacts
# $ gam select DomainB redirect csv ./DomainBUsers.csv print users fields primaryemail,name
# $ gam select DomainA redirect csv ./DomainAContacts.csv print contacts fields email,familyname,givenname,name emailmatchpattern ".*@DomainB.com$"
# 2: Find add, deletes, updates for DomainA domain shared contacts
# $ python3 ExchangeDomainSharedContacts.py DomainB.com ./DomainBUsers.csv ./DomainAContacts.csv ./DomainAContactUpdates.csv
$ 3: Perform updates for DomainA domain shared contacts
# $ gam select DomainA redirect stdout ./DomainAContactDeletes.txt redirect stderr stdout loop ./DomainAContactUpdates.csv matchfield Action Delete gam delete contact "~ContactID"
# $ gam select DomainA redirect stdout ./DomainAContactUpdates.txt redirect stderr stdout loop ./DomainAContactUpdates.csv matchfield Action Update gam update contact "~ContactID" givenName "~givenName" familyname "~familyName" name "~fullName"
# $ gam select DomainA redirect stdout ./DomainAContactAdds.txt redirect stderr stdout loop ./DomainAContactUpdates.csv matchfield Action Add gam create contact email work '~ContactID' primary givenName "~givenName" familyname "~familyName" name "~fullName"
# Make DomainA users be domain shared contacts in DomainB
# 1: Generate the two data files, DomainA users and DomainB domain shared contacts
# $ gam select DomainA redirect csv ./DomainAUsers.csv print users fields primaryemail,name
# $ gam select DomainB redirect csv ./DomainBContacts.csv print contacts fields email,familyname,givenname,name emailmatchpattern ".*@DomainA.com$"
# 2: Find add, deletes, updates for DomainB domain shared contacts
# $ python3 ExchangeDomainSharedContacts.py DomainA.com ./DomainAUsers.csv ./DomainBContacts.csv ./DomainBContactUpdates.csv
$ 3: Perform updates for DomainB domain shared contacts
# $ gam select DomainB redirect stdout ./DomainBContactDeletes.txt redirect stderr stdout loop ./DomainBContactUpdates.csv matchfield Action Delete gam delete contact "~ContactID"
# $ gam select DomainB redirect stdout ./DomainBContactUpdates.txt redirect stderr stdout loop ./DomainBContactUpdates.csv matchfield Action Update gam update contact "~ContactID" givenName "~givenName" familyname "~familyName" name "~fullName"
# $ gam select DomainB redirect stdout ./DomainBContactAdds.txt redirect stderr stdout loop ./DomainBContactUpdates.csv matchfield Action Add gam create contact email work '~ContactID' primary givenName "~givenName" familyname "~familyName" name "~fullName"
"""
import csv
import re
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
CONTACTID = 'ContactID'
GIVENNAME = 'givenName'
FAMILYNAME = 'familyName'
FULLNAME = 'fullName'
USE_REGEX_FOR_DOMAIN_MATCH = False # False: string match, True: regex match
userDomains = set(sys.argv[1].lower().replace(',', ' ').split())
if USE_REGEX_FOR_DOMAIN_MATCH:
domainPatterns = []
for domain in userDomains:
try:
domainPatterns.append(re.compile(domain))
except re.error as e:
sys.stderr.write(f'ERROR: {domain} invalid, {str(e)}\n')
sys.exit(1)
userData = {}
userSet = set()
with open(sys.argv[2], 'r', encoding='utf-8') as inputFile:
for user in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
emailAddress = user['primaryEmail'].lower()
_, domain = emailAddress.split('@')
if not USE_REGEX_FOR_DOMAIN_MATCH:
if domain in userDomains:
userData[emailAddress] = {GIVENNAME: user[f'name.{GIVENNAME}'], FAMILYNAME: user[f'name.{FAMILYNAME}'],
FULLNAME: user[f'name.{FULLNAME}']}
userSet.add(emailAddress)
else:
for pattern in domainPatterns:
if pattern.match(domain):
userData[emailAddress] = {GIVENNAME: user[f'name.{GIVENNAME}'], FAMILYNAME: user[f'name.{FAMILYNAME}'],
FULLNAME: user[f'name.{FULLNAME}']}
userSet.add(emailAddress)
break
contactData = {}
contactSet = set()
with open(sys.argv[3], 'r', encoding='utf-8') as inputFile:
for contact in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
emailAddress = contact['Emails.1.address'].lower()
_, domain = emailAddress.split('@')
if domain in userDomains:
contactData[emailAddress] = {GIVENNAME: contact['Given Name'], FAMILYNAME: contact['Family Name'],
FULLNAME: contact['Name'], CONTACTID: contact[CONTACTID]}
contactSet.add(emailAddress)
addContacts = userSet-contactSet
delContacts = contactSet-userSet
chkContacts = contactSet-delContacts
with open(sys.argv[4], 'w', encoding='utf-8', newline='') as outputFile:
outputCSV = csv.DictWriter(outputFile, ['Action', CONTACTID, GIVENNAME, FAMILYNAME, FULLNAME],
lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for emailAddress in sorted(delContacts):
contact = contactData[emailAddress]
outputCSV.writerow({'Action': 'Delete',
CONTACTID: contact[CONTACTID],
GIVENNAME: contact[GIVENNAME],
FAMILYNAME: contact[FAMILYNAME],
FULLNAME: contact[FULLNAME]})
for emailAddress in sorted(chkContacts):
user = userData[emailAddress]
contact = contactData[emailAddress]
for field in [GIVENNAME, FAMILYNAME, FULLNAME]:
if user[field] != contact[field]:
outputCSV.writerow({'Action': 'Update',
CONTACTID: contact[CONTACTID],
GIVENNAME: user[GIVENNAME],
FAMILYNAME: user[FAMILYNAME],
FULLNAME: user[FULLNAME]})
break
for emailAddress in sorted(addContacts):
user = userData[emailAddress]
outputCSV.writerow({'Action': 'Add',
CONTACTID: emailAddress,
GIVENNAME: user[GIVENNAME],
FAMILYNAME: user[FAMILYNAME],
FULLNAME: user[FULLNAME]})