#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
check_days = 15
import ctypes
from subprocess import PIPE, Popen
from datetime import datetime, date
import json
import os
def ecmd(command):
    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)

    with disable_file_system_redirection():
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    if ret==0:
        if out:
            return out.strip()
        else:
            return ret
    else:
        if err:
            return err.strip()
        else:
            return ret

gt=[]

try:
    wdir=os.environ['PROGRAMDATA']+'\\temp'
    if not os.path.exists(wdir):
        os.mkdir(wdir)
except:
    wdir=os.environ['SYSTEMDRIVE']

fp=wdir+'\\users.txt'
cmd="wmic useraccount get name"
obj=Popen(cmd, shell=True, stdout = PIPE, stderr = PIPE)
out, err = obj.communicate()

with open(fp, 'w+') as dr:
    dr.write(out)

with open(fp, 'r+') as dr:
    for i in dr: None if 'Name' in i or 'DefaultAccount' in i or 'Guest' in i or 'Administrator' in i or 'WDAGUtilityAccount' in i else gt.append(i.strip())
print gt
for user in gt:
    comd = ecmd(r'net user %s | findstr /B /C:"Last logon"'%user)
    if comd != 1:
        if 'Last logon' in comd and 'Never' not in comd:
            comd = comd.split(' ')
            if comd[-1] in ('AM','PM'):
                login_date = comd[-3]
            else:
                login_date = comd[-2]
            currentMonth = datetime.now().month
            if '-' in login_date:
                date_split = login_date.split('-')
                currentMonth = '{:02d}'.format(currentMonth)
                month = ''
                date = ''
                year = ''
                date_format = ''
                if int(currentMonth) == int(date_split[0]) or int(currentMonth) == int(date_split[1]) or int(currentMonth) == int(date_split[2]):
                    if int(date_split[0]) == int(currentMonth) and len(date_split[2]) == 4:
                        date_format = '%m-%d-%Y'
                    elif int(date_split[1]) == int(currentMonth) and len(date_split[2]) == 4:
                        date_format = '%d-%m-%Y'
                    elif int(date_split[2]) == int(currentMonth) and len(date_split[0]) == 4:
                        date_format = '%Y-%d-%m'
                    elif int(date_split[2]) == int(currentMonth) and len(date_split[1]) == 4:
                        date_format = '%d-%Y-%m'
                    elif int(date_split[1]) == int(currentMonth) and len(date_split[0]) == 4:
                        date_format = '%Y-%m-%d'
                    elif int(date_split[0]) == int(currentMonth) and len(date_split[1]) == 4:
                        date_format = '%m-%Y-%d'

                elif (int(currentMonth) - 1) == int(date_split[0]) or (int(currentMonth) - 1) == int(date_split[1]) or (int(currentMonth) - 1) == int(date_split[2]):
                    if int(date_split[0]) == (int(currentMonth) - 1) and len(date_split[2]) == 4:
                        date_format = '%m-%d-%Y'
                    elif int(date_split[1]) == (int(currentMonth) - 1) and len(date_split[2]) == 4:
                        date_format = '%d-%m-%Y'
                    elif int(date_split[2]) == (int(currentMonth) - 1) and len(date_split[0]) == 4:
                        date_format = '%Y-%d-%m'
                    elif int(date_split[2]) == (int(currentMonth) - 1) and len(date_split[1]) == 4:
                        date_format = '%d-%Y-%m'
                    elif int(date_split[1]) == (int(currentMonth) - 1) and len(date_split[0]) == 4:
                        date_format = '%Y-%m-%d'
                    elif int(date_split[0]) == (int(currentMonth) - 1) and len(date_split[1]) == 4:
                        date_format = '%m-%Y-%d'
            else:
                date_split = login_date.split('/')
                currentMonth = '{:02d}'.format(currentMonth)
                month = ''
                date = ''
                year = ''
                date_format = ''
                if int(currentMonth) == int(date_split[0]) or int(currentMonth) == int(date_split[1]) or int(currentMonth) == int(date_split[2]):
                    if int(date_split[0]) == int(currentMonth) and len(date_split[2]) == 4:
                        date_format = '%m/%d/%Y'
                    elif int(date_split[1]) == int(currentMonth) and len(date_split[2]) == 4:
                        date_format = '%d/%m/%Y'
                    elif int(date_split[2]) == int(currentMonth) and len(date_split[0]) == 4:
                        date_format = '%Y/%d/%m'
                    elif int(date_split[2]) == int(currentMonth) and len(date_split[1]) == 4:
                        date_format = '%d/%Y/%m'
                    elif int(date_split[1]) == int(currentMonth) and len(date_split[0]) == 4:
                        date_format = '%Y/%m/%d'
                    elif int(date_split[0]) == int(currentMonth) and len(date_split[1]) == 4:
                        date_format = '%m/%Y/%d'

                elif (int(currentMonth) - 1) == int(date_split[0]) or (int(currentMonth) - 1) == int(date_split[1]) or (int(currentMonth) - 1) == int(date_split[2]):
                    if int(date_split[0]) == (int(currentMonth) - 1) and len(date_split[2]) == 4:
                        date_format = '%m/%d/%Y'
                    elif int(date_split[1]) == (int(currentMonth) - 1) and len(date_split[2]) == 4:
                        date_format = '%d/%m/%Y'
                    elif int(date_split[2]) == (int(currentMonth) - 1) and len(date_split[0]) == 4:
                        date_format = '%Y/%d/%m'
                    elif int(date_split[2]) == (int(currentMonth) - 1) and len(date_split[1]) == 4:
                        date_format = '%d/%Y/%m'
                    elif int(date_split[1]) == (int(currentMonth) - 1) and len(date_split[0]) == 4:
                        date_format = '%Y/%m/%d'
                    elif int(date_split[0]) == (int(currentMonth) - 1) and len(date_split[1]) == 4:
                        date_format = '%m/%Y/%d'
            
            if date_format:
                date_obj = datetime.strptime(login_date, date_format).date()
                today = datetime.now().date()
                diff = today - date_obj
                diff_days = diff.days
                if diff_days > check_days:
                    cmd="net user "+user+" /delete"
                    print os.popen(cmd).read()
                    try:
                        path="C:\\Users\\"+user
                        obj=sp.Popen("rmdir /Q/S "+path,stdin=sp.PIPE,stdout=sp.PIPE,shell=True)
                        print(obj.communicate()[0])
                        obj3=sp.Popen("hostname",stdin=sp.PIPE,stdout=sp.PIPE)
                        computername=obj3.communicate()[0].strip()
                        obj2=sp.Popen("Get-CimInstance -ComputerName" +computername+"  -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq "+user+" } | Remove-CimInstance")
                        print obj2.communicate()[0]
                        print "Done"
                    except:
                        pass
        else:
            print "No Data Found"
    else:
        print "No Data Found"
