-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disable_expired_accounts.py
51 lines (38 loc) · 1.65 KB
/
Disable_expired_accounts.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
import pyodbc
import subprocess
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
#Connection to DB
server = ''
database = ''
username = ''
password = ''
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
#Get expired accounts for Docenti
cursor.execute("SELECT [sAMAccountName] ,CONVERT(DATE, left([accountExpires],16), 102) as accountExpires, GETDATE(),[pager] FROM [].[dbo].[] where [accountExpires] not like '1601%' and CONVERT(DATE, left([accountExpires],16),102) < GETDATE() and [pager] = 'AzureSyncedUser' ORDER BY accountExpires;")
#Get all rows of the query
docenti = cursor.fetchall()
#Break counter
i=0
for row in docenti :
print(row[0])
#row = cursor.fetchone()
subprocess.run(['powershell.exe', '-noprofile' ,'-c', '.\Disable_expired_accounts.ps1 @("'+row[0]+'")'])
i+=1
#Safety first
if i == 2:
break
#Get expired accounts for Alumni
cursor.execute("SELECT [sAMAccountName] ,CONVERT(DATE, left([accountExpires],16), 102) as accountExpires, GETDATE(),[pager] FROM [].[dbo].[] where [accountExpires] not like '1601%' and CONVERT(DATE, left([accountExpires],16),102) < GETDATE() and [pager] = 'AzureSyncedUser' ORDER BY accountExpires;")
#Get all rows of the query
alumni = cursor.fetchall()
#Break counter
i=0
for row in alumni :
print(row[0])
subprocess.run(['powershell.exe', '-noprofile' ,'-c', '.\Disable_expired_accounts.ps1 @("'+row[0]+'")'])
i+=1
#Safety first
if i == 2:
break