ADD Exchange User disable to disable_user.py
This commit is contained in:
+31
-17
@@ -25,25 +25,37 @@ def getdabase():
|
||||
return r.text.replace('"','')
|
||||
|
||||
# PowerShell-Skript für die Active Directory-Abfrage
|
||||
# powershell_script = r'''
|
||||
# # PowerShell-Skript für die Active Directory-Abfrage mit Filter
|
||||
# $ADResults = Get-ADUser -Filter { (ObjectClass -eq "user") -and (-not (UserPrincipalName -like "*Mailbox*")) -and (-not (UserPrincipalName -like "*Admin*")) -and (-not (UserPrincipalName -like "*Exchange*"))} -Properties SamAccountName | Select-Object SamAccountName
|
||||
#
|
||||
# # Ausgabe der JSON-Daten
|
||||
# $ADResults | ConvertTo-Json | Out-File -FilePath "disable_user.json" -Encoding UTF8
|
||||
# '''
|
||||
powershell_script = r'''
|
||||
# PowerShell-Skript für die Active Directory-Abfrage mit Filter
|
||||
$ADResults = Get-ADUser -Filter { (ObjectClass -eq "user") -and (-not (UserPrincipalName -like "*Mailbox*")) -and (-not (UserPrincipalName -like "*Admin*")) -and (-not (UserPrincipalName -like "*Exchange*"))} -Properties SamAccountName | Select-Object SamAccountName
|
||||
|
||||
def datum_in_filetime(used_date):
|
||||
jahr = used_date.year
|
||||
monat = used_date.month
|
||||
datum = datetime(jahr, monat + 1, 1) if monat < 12 else datetime(jahr + 1, 1, 1)
|
||||
return datum
|
||||
# Ausgabe der JSON-Daten
|
||||
$ADResults | ConvertTo-Json | Out-File -FilePath "disable_user.json" -Encoding UTF8
|
||||
'''
|
||||
|
||||
print("FileTime-Wert für den ersten Tag des nächsten Monats:", datum_in_filetime(datetime(2023, 3, 23)))
|
||||
print(getdabase())
|
||||
|
||||
def erster_tag_folgender_monat(date_str):
|
||||
date_obj = datetime.strptime(date_str, "%d.%m.%Y")
|
||||
if date_obj.month == 12:
|
||||
next_month = 1
|
||||
next_year = date_obj.year + 1
|
||||
else:
|
||||
next_month = date_obj.month + 1
|
||||
next_year = date_obj.year
|
||||
erster_tag_folgender_monat = datetime(next_year, next_month, 1)
|
||||
result_str = erster_tag_folgender_monat.strftime("%d.%m.%Y")
|
||||
return result_str
|
||||
|
||||
def exchange_disable(samaccountname,datum):
|
||||
powershell_script = fr'''$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://tnpex01/PowerShell/ -Authentication Kerberos
|
||||
Import-PSSession $Session
|
||||
Set-Mailbox -Identity {samaccountname} -AccountDisabled $true
|
||||
'''
|
||||
subprocess.run(["powershell", "-command", powershell_script], stdout=subprocess.PIPE, text=True)
|
||||
|
||||
# PowerShell-Skript ausführen und die Ausgabe abrufen
|
||||
# result = subprocess.run(["powershell", "-command", powershell_script], stdout=subprocess.PIPE, text=True)
|
||||
result = subprocess.run(["powershell", "-command", powershell_script], stdout=subprocess.PIPE, text=True)
|
||||
|
||||
with open('disable_user.json', 'r',encoding='utf-8-sig') as file:
|
||||
ad_data = json.load(file)
|
||||
@@ -66,7 +78,6 @@ with open('disable_user.json', 'r',encoding='utf-8-sig') as file:
|
||||
if datum is None:
|
||||
next
|
||||
else:
|
||||
print(f"{datum} - {gruppen} - {samaccountname}")
|
||||
print(subprocess.run(["powershell", "-command", f"Remove-ADGroupMember -Identity '{gruppen}' -Members '{samaccountname}' -Confirm:$False"]))
|
||||
if gruppen == "G-RDP-User":
|
||||
count +=1
|
||||
@@ -75,5 +86,8 @@ with open('disable_user.json', 'r',encoding='utf-8-sig') as file:
|
||||
count +=1
|
||||
print(subprocess.run(["powershell", "-command",
|
||||
f"Set-ADUser -Identity '{samaccountname}'" "-Replace @{extensionAttribute2 = 'Exchange - Deaktiviert am: "f'{datum}'"'} -Confirm:$False"]))
|
||||
if count == 2:
|
||||
print(count)
|
||||
print(f"Set-Mailbox -Identity {samaccountname} -CustomAttribute1 'Deaktiviert am: {datum}'")
|
||||
exchange_disable(samaccountname,datum)
|
||||
if count >= 2:
|
||||
print(subprocess.run(["powershell", "-command",
|
||||
f"Set-ADUser -Identity '{samaccountname}'" "-Replace @{Description = 'Deaktiviert zum: "f'{erster_tag_folgender_monat(datum)}'"'} -Confirm:$False"]))
|
||||
Reference in New Issue
Block a user