#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
import os
import ctypes
import re
import time
import _winreg
from _winreg import *
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)
def check():
    
    with disable_file_system_redirection():
        inst=os.popen("wmic product get name,identifyingnumber").read()
         
        
    return inst

def uninstall (find):
	command="MsiExec.exe /X "+find+" /qn"
	uninst=os.popen(command).read()
	time.sleep(120)
	inst=check()
	find=re.findall('{.*}\s\sKaspersky\sEndpoint\sSecurity\sfor\sWindows',inst)
	if len(find) >0:
		print "\t*)Kaspersky Endpoint Security for Windows is not uninstalled on Endpoint"
		print "\t*)Please restart the computer and Try again"
	else :
		print "\t*)Kaspersky Endpoint Security for Windows has been Uninstalled Successfully"

def uninstall1 (find):
	command="MsiExec.exe /X "+find+" /qn"
	uninst=os.popen(command).read()
	time.sleep(120)
	inst=check()
	find=re.findall('{.*}\s\sKaspersky\sSecurity\sCenter\s10\sNetwork\sAgent',inst)
	if len(find) >0:
		print "\t*)Kaspersky Security Center 10 Network Agent is not uninstalled on Endpoint"
		print "\t*)Please restart the computer and Try again"
	else :
		print "\t*)Kaspersky Security Center 10 Network Agent has been Uninstalled Successfully"

inst=check()
 
if len(inst)>0:
    find=re.findall('{.*}\s\sKaspersky\sEndpoint\sSecurity\sfor\sWindows',inst)
    if len(find)>0:
        final=re.findall('{.*}',find[0])[0]
        if len(final) == 38:
            print "GUID for Kaspersky Endpoint Security for Windows is :"+final
            print "\t*)Kaspersky Endpoint Security for Windows is installed on Endpoint"
            print "\t*)Uninstalling has started "
            uninstall(final)
    else:
        print "\t*)Kaspersky Endpoint Security for Windows is not installed at End point"

inst1=check()
 
if len(inst1)>0:
    find=re.findall('{.*}\s\sKaspersky\sSecurity\sCenter\s10\sNetwork\sAgent',inst1)
    if len(find)>0:
        final=re.findall('{.*}',find[0])[0]
        if len(final) == 38:
            print "\n\n\nGUID for Kaspersky Security Center 10 Network Agent is :"+final
            print "\t*)Kaspersky Security Center 10 Network Agent is installed on Endpoint"
            print "\t*)Uninstalling has started "
            uninstall1(final)
    else:
        print "\t*)Kaspersky Security Center 10 Network Agent is not installed at End point"
