-
Notifications
You must be signed in to change notification settings - Fork 0
/
adTool.py
executable file
·80 lines (68 loc) · 3.08 KB
/
adTool.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
import configparser
from time import sleep
from pathlib import Path
from .psbinding import PowerShellPipe
from .exchTool import Exchange
from .helpers import password, translit
config_defaults = {'domain':'contoso.com',
'ad_path':'OU=Users,DC=contoso,DC=com'}
adTool_config_path = 'adTool.ini'
config = configparser.ConfigParser(defaults = config_defaults)
if not Path(adTool_config_path).is_file():
open(adTool_config_path,'w').close()
config.read(adTool_config_path)
try:
cfg_password = config['DEFAULT']['password']
except KeyError:
cfg_password = password()
print(f"'password' value in section 'DEFAULT' from {adTool_config_path} not found. Using '{cfg_password}'.")
with open(adTool_config_path, 'w') as f:
config.write(f)
class User():
def __init__(self,company='',name='',sername='',fullname='',otchestvo = '',subunit='',post='',pswd='',tel=''):
self.name = name
self.sername = sername
self.fullname = fullname
self.otchestvo = otchestvo
self.subunit = subunit
self.post = post
self.pswd = pswd
self.tel = tel
self.company = company
@property
def check(self):
return all([self.sername])#,self.name,self.account,self.password,self.fullname,self.otchestvo])
@property
def account(self):
initials = translit(self.name[0])[0] if self.name else ''
initials += translit(self.otchestvo[0])[0] if self.otchestvo else ''
account = translit(self.sername)
account += '.'+initials if initials else ''
return account
@property
def password(self):
if self.pswd:
return self.pswd
else:
return cfg_password
@property
def sAMAccountName(self):
if len(self.sername + ' ' + self.name) > 20:
return self.sername + ' ' + self.name[0].upper() + self.otchestvo[0].upper()
else:
return self.sername + ' ' + self.name if self.name else self.sername
def __str__(self):
return str(vars(self))
class AD():
def __init__(self):
self.ps = PowerShellPipe()
self.__connect()
def __connect(self):
self.ps.send(f'''import-module ActiveDirectory''')
return self.ps.read(10)
def get_user(self,account):
self.ps.send(f"Get-ADUser -F {{SamAccountName -eq '{account}'}}")
return self.ps.read(10)
def new_user(self,user):
self.ps.send(f'''New-ADUser -sAMAccountName '{user.sAMAccountName}' -Name '{user.sername} {user.name} {user.otchestvo}' -GivenName '{user.name}' -Surname '{user.sername}' -DisplayName '{user.fullname}' -Description '{user.post}' -Title '{user.post}' -Company '{user.company}' -Department '{user.subunit}' -UserPrincipalName '{user.account}@{config['DEFAULT']['domain']}' -Enabled $True -OfficePhone '{user.tel}' -Path '{config['DEFAULT']['ad_path']}' -AccountPassword (ConvertTo-SecureString -AsPlainText {user.password} -force)''')
return self.ps.read(10)