appname = "Endpoint Manager"
import os
import re
import subprocess
import ctypes
from datetime import datetime, timedelta

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():        
    uninstall_string = []
    reg_path = ['HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall','HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall']
    for i in reg_path:
        if len(uninstall_string) > 0:
            break
        else:
            cmd = 'Reg Query "'+i+'"'
            obj = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
            out, err = obj.communicate()
            if err:
                print(err)
            else:
                out = out.strip().splitlines()
                for j in out:
                    if len(uninstall_string) > 0:
                        break
                    else:
                        cmd_2 = 'Reg Query "'+j.strip()+'" /v DisplayName'
                        obj_2 = subprocess.Popen(cmd_2, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
                        out_2, err_2 = obj_2.communicate()
                        if err_2:
                            pass 
                        else:
                            out_2 = out_2.strip().split('REG_SZ')[-1].strip()
                            if appname in out_2:
                                cmd_3 = 'Reg Query "'+j.strip()+'" /v UninstallString'
                                obj_3 = subprocess.Popen(cmd_3, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
                                out_3, err_3 = obj_3.communicate()
                                if err_3:
                                    pass 
                                else:
                                    out_3 = out_3.strip().split('REG_EXPAND_SZ')[-1].strip()
                                    uninstall_string.append(out_3)
                                break
                            else:
                                pass 

    if len(uninstall_string) > 0:
        uninstall_id = re.findall('{..+',uninstall_string[-1])[-1]
        command = 'msiexec /quiet /norestart /uninstall '+uninstall_id+' /L*V "C:\\Windows\\Temp\\uninstall_log.txt"'
        user = os.popen('whoami').read().strip()
        now = datetime.now()
        result = now + timedelta(minutes=2) 
        TIME = str(result.strftime('%H:%M'))
        cmd = 'schtasks /create /tn Endpoint_Uninstall /tr "C:\\Windows\\System32\\'+command+'" /sc once /st '+TIME+' /RU "System" /f'
        obj = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        out, err = obj.communicate()
        if err:
            print(err)
            print("Unable to Uninstall Endpoint Manager Communication Client")
        else:
            print(out_2+' Will be uninstalled at '+TIME)

    else:
        print("Unable to Uninstall Endpoint Manager Communication Client")

