#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
emailto=itsm.getParameter('EmailTo')  ## Provide an Toemail address where the mail need to be sent.You can also provide any number of To eamil address For example: ['varun@yopmail.com', xxxx@yopmail.com]
emailfrom=itsm.getParameter('EmailFrom')  ## Provide the From Email address from which the mail to be send
password=itsm.getParameter('Password')               ##Provide password for from email
smtpserver='smtp.gmail.com'
port=587

import os,sys,shutil,re,sys,socket,_winreg,random,getpass
from datetime import date, datetime
import socket
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import ctypes
from subprocess import PIPE, Popen

def computername():
    return os.environ['COMPUTERNAME']

def ipaddress():
    return socket.gethostbyname(socket.gethostname())

subject='%s %s Alert : Sfc Scan Report!!!!!'%(computername(), ipaddress())		

def emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody,fileToSend):
    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = ",".join(emailto)
    msg["Subject"] = subject
    msg.preamble = subject
    body = MIMEText(msgbody)
    msg.attach(body)    
    for file in fileToSend:
        with open(file) as fp:
            record = MIMEBase('application', 'octet-stream')
            record.set_payload(fp.read())
            encoders.encode_base64(record)
            record.add_header('Content-Disposition', 'attachment',
            filename=os.path.basename(file))
            msg.attach(record)
    try:
        server = smtplib.SMTP(smtpserver,port)
        server.ehlo()
        server.starttls()
        server.login(emailfrom, password)
        server.sendmail(emailfrom, emailto, msg.as_string())
        server.quit()
        return " \nThe email report has been sent to "+msg["To"]
    except Exception as e:
        return e

command=r'sfc /scannow' #Please edit your comand here.
restore_health_cmd = 'DISM /Online /Cleanup-Image /RestoreHealth'
scan_health_cmd = 'DISM /Online /Cleanup-image /Scanhealth'

def ecmd(command):
    obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    if out:
        output = out.replace('  ', '   ')[::2]
        print output
        if 'Windows Resource Protection did not find any integrity violations.' in output:
            msgbody = output
            fileToSend = []
            print emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody,fileToSend)
        elif 'Windows Resource Protection found corrupt files and successfully repaired them.' in output:
            msgbody = output
            fileToSend = ['C:\Windows\Logs\CBS\CBS.log']
            print emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody,fileToSend)
        elif 'Windows Resource Protection found corrupt files but was unable to fix some of them.' in output:
            re_obj = Popen(restore_health_cmd, shell = True, stdout = PIPE, stderr = PIPE)
            re_out, re_err = re_obj.communicate()
            sch_obj = Popen(scan_health_cmd, shell = True, stdout = PIPE, stderr = PIPE)
            sch_out, sch_err = sch_obj.communicate()
            sc_obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
            sc_out, sc_err = sc_obj.communicate()
            msgbody = output
            fileToSend = ['C:\Windows\Logs\CBS\CBS.log', 'C:\Windows\Logs\DISM\dism.log']
            print emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody,fileToSend)
        else:
            msgbody = output
            fileToSend = []
            print emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody,fileToSend)
    else:
        print err
		
ecmd(command)
