Note:
RUN AS LOCALSYSTEM USER.
The Bitlocker deactivated status will be mailed to the sender's Mail (EmailTo)
1.Provide the ToEmail address in the parameter 'EmailTo' where the mail need to be sent. the datatype should be a string.
2.Provide the From Email (Gmail) address in the parameter 'EmailFrom' from which the mail to be send. the datatype should be a string.
3.Provide application password for from email in the parameter 'Password'. the datatype should be a string.
4.Provide mail flag in the parameter 1 or 0 (1 - outlook, 0 - gmail). the datatype should be a int.
Gmail
use this link to generate application password https://security.google.com/settings/security/apppasswords
1) Turn on Two-step verification ( https://security.google.com/settings/security )
2) select other in "select app" section
3) give any app name
4) select generate and use the 16 digit code as application password instead of email password
Outlook
use this link to generate application password https://account.microsoft.com/security
1) Select Advance security options
2) Turn on Two-step verification
3) After completing Two-step verification scroll down in Advance security options page for App password Section
4) Select create a app password and use the 16 digit code as application password instead of email password
import ctypes
import re
import socket
import subprocess
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
Receiver = itsm.getParameter('EmailTo') #Provide a Toemail address where the mail need to be sent. the datatype should be a string.
Sender = itsm.getParameter('EmailFrom') #Provide the From Email address from which the mail to be send. the datatype should be a string.
Password = itsm.getParameter('Password') #Provide app password for from email. the datatype should be a string.
MailFlag = itsm.getParameter('MailFlag') #Provide mail flag 1 or 0 (1 - outlook, 0 - gmail). the datatype should be a int.
def gmail(sender_email, password, receiver, text):
try:
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver
msg["Subject"] = "Bitlocker Status"
msg.attach(MIMEText(text, 'plain'))
if MailFlag:
server = smtplib.SMTP("smtp.office365.com", 587)
else:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver, msg.as_string())
server.quit()
return "Bitlocker Status has been mailed successfully"
except Exception as e:
return e
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
Cmd = 'C:\\Windows\\System32\\manage-bde.exe -status'
Drvlist = 'fsutil fsinfo drives'
def executeCmd(ExcCMD):
with disable_file_system_redirection():
process = subprocess.Popen(ExcCMD, shell=True, stdout=subprocess.PIPE)
result = process.communicate()[0]
return result
excResult = ''
getDisk = []
driverList = executeCmd(Drvlist).split(" ")[1:-1]
for dsk in driverList:
DiskStat = executeCmd(Cmd + " " + dsk.replace('\\', ''))
if 'Protection Off' in DiskStat and 'Unlocked' in DiskStat:
getDisk.append(dsk.replace('\\', ''))
if excResult == '':
excResult = excResult + DiskStat
else:
excResult = excResult + re.sub('BitLocker .+reserved.\r\n\r\n', '', DiskStat, flags=re.DOTALL)
if excResult == '':
excResult = executeCmd(Cmd)
def getBitStatus(getlist):
BitlockerStat = ""
if len(getlist) != 1:
for i in range(len(getlist)):
if i == 0:
BitlockerStat = BitlockerStat + 'Bitlocker for ' + getlist[i]
elif i != len(getlist) - 1:
BitlockerStat = BitlockerStat + ", " + getlist[i]
elif i == len(getlist) - 1:
BitlockerStat = BitlockerStat + " and " + getlist[i] + " drive is deactivated\n\n"
elif len(getlist) == 1:
BitlockerStat = BitlockerStat + "Bitlocker for " + getlist[0] + " drive is deactivated\n\n"
elif len(getlist) == 0:
BitlockerStat = BitlockerStat + 'Bitlocker is active\n\n'
return BitlockerStat
content = socket.gethostname() + "\t\t" + datetime.now().strftime("LAST STATUS UPDATE: %m/%d/%y %H:%M:%S %p") \
+ "\n\n" + getBitStatus(getDisk) + excResult
if len(getDisk) != 0:
print str(gmail(Sender, Password, Receiver, content)) + '\n'
print content
Comments