This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_to_mxv.py
More file actions
67 lines (51 loc) · 2.61 KB
/
Copy pathazure_to_mxv.py
File metadata and controls
67 lines (51 loc) · 2.61 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
import json
import csv
import secrets
import string
from subprocess import check_output, call
fieldnames = ("Azure AD Group","First Name","Last Name","Extension","Voice DID","Fax DID","Caller ID","ID for MS Exchange","Home Phone","Cell Phone",
"Fax Number", "E-mail","Alternate E-mail","User Name","Password","PIN","Pseudonym","User Profile","ID","Admin Profile","Paging Profile",
"Recording Profile", "Home MX","Current MX", "Default Role","Assigned Device(s)","CallGroup","AA")
password_length = 8
pin_length = 6
def generate_password (N):
return ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(N))
def generate_pin (N):
return ''.join(secrets.choice(string.digits) for _ in range(N))
def main():
# ID and extension format, increments by one for every new user
id_number = 100
extension_number = 100
# Login
call(['az', 'login'])
# Get Azure user list
user_list_json = json.loads(check_output(['az', 'ad', 'user', 'list']).decode('utf-8'))
# CSV output
with open("csv_output.csv","w") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, dialect='excel')
writer.writeheader()
for user in user_list_json:
extension_number += 1
id_number += 1
user_group_list = check_output(['az', 'ad', 'user', 'get-member-groups', '--upn-or-object-id', user['userPrincipalName'], '--query', '[].displayName']).decode('utf-8')
# Formatting names
user_name = user['givenName']
user_surname = user['surname']
if user_name is None and user_surname is None:
if user['displayName'] is not None:
user_name, *middle_name, user_surname = user['displayName'].split()
# Write to CSV file
writer.writerow({"Azure AD Group" : user_group_list,
"First Name" : user_name,
"Last Name" : user_surname,
"Home Phone" : user['telephoneNumber'],
"Cell Phone" : user['mobile'],
"E-mail" : user['userPrincipalName'],
"User Name" : user['userPrincipalName'].split("@")[0],
"Password" : generate_password(password_length),
"PIN" : generate_pin(pin_length),
"Extension" : extension_number,
"ID" : id_number
})
if __name__ == '__main__':
main()