'ManageEngine Security Manager Plus Agent .

'Script to Clean up ManageEngine Security Manager Plus Agent from Add remove programs .
'======================================================================================

On Error Resume Next

'Removing the Agent from Add Remove Programs (if uninstallation failed)
'======================================================================
Err.Clear
Set WshShell = WScript.CreateObject("WScript.Shell")

   WshShell.RegRead("HKEY_CLASSES_ROOT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\")
   WshShell.RegDelete "HKEY_CLASSES_ROOT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\"
   WshShell.RegDelete "HKEY_CLASSES_ROOT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Media\"
   WshShell.RegDelete "HKEY_CLASSES_ROOT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\"
   WshShell.RegDelete "HKEY_CLASSES_ROOT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\"

   WshShell.RegRead ("HKEY_CLASSES_ROOT\Installer\Features\FF293FAE659FE9947A27F8F2F21D4EE5\")
   WshShell.RegDelete "HKEY_CLASSES_ROOT\Installer\Features\FF293FAE659FE9947A27F8F2F21D4EE5\"


   WshShell.RegRead("HKEY_CURRENT_USER\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\")
   WshShell.RegDelete "HKEY_CURRENT_USER\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\"
   WshShell.RegDelete "HKEY_CURRENT_USER\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Media\"
   WshShell.RegDelete "HKEY_CURRENT_USER\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\"
   WshShell.RegDelete "HKEY_CURRENT_USER\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\"

   WshShell.RegRead("HKEY_USERS\.DEFAULT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\")
   WshShell.RegDelete "HKEY_USERS\.DEFAULT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Net\"
   WshShell.RegDelete "HKEY_USERS\.DEFAULT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\Media\"
   WshShell.RegDelete "HKEY_USERS\.DEFAULT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\SourceList\"
   WshShell.RegDelete "HKEY_USERS\.DEFAULT\Installer\Products\FF293FAE659FE9947A27F8F2F21D4EE5\"

   WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Usage\")
   WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Usage\"
   WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Patches\")
   WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Patches\"
   WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\InstallProperties\")
   WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\InstallProperties\"
   WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Features\")
   WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\Features\"
   WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\")
   WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\FF293FAE659FE9947A27F8F2F21D4EE5\"


  WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EAF392FF-F956-499E-A772-8F2F2FD1E45E}\")
  WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EAF392FF-F956-499E-A772-8F2F2FD1E45E}\"


 'msgbox "ManageEngine Unified Endpoint Security - Agent Remove Add Remove Programs Entry Status " & Err.Number


'Uninstall Agent Service (if uninstallation failed)
'===================================================
Err.Clear
WshShell.Run "%windir%\system32\sc control "&Chr(34)& "ManageEngine Unified Endpoint Security - Agent" &Chr(34)& " 193",0,True
WshShell.Run "%windir%\system32\sc stop "   &Chr(34)& "ManageEngine Unified Endpoint Security - Agent"  &Chr(34),0,True
WshShell.Run "%windir%\system32\sc delete " &Chr(34)& "ManageEngine Unified Endpoint Security - Agent"  & Chr(34),0,True

'msgbox "ManageEngine Security Manager Plus Agent uninstallation  service " & Err.Number

'Get the Agent Installed directory and Registry location details
'===============================================================
Err.Clear
checkOSArch = WshShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")

'Wscript.Echo checkOSArch

if Err Then
	Err.Clear
	'WScript.Echo "The OS Architecture is unable to find ,so it was assumed to be 32 bit"
	regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\AdventNet\ManageEngine\UESAgent"
	subKey = "SOFTWARE\AdventNet\ManageEngine\UESAgent"
else
	if checkOSArch = "x86" Then
		'Wscript.Echo "The OS Architecture is 32 bit"
		regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\AdventNet\ManageEngine\UESAgent"
		subKey = "SOFTWARE\AdventNet\ManageEngine\UESAgent"
	else
		'Wscript.Echo "The OS Architecture is 64 bit"
		regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\AdventNet\ManageEngine\UESAgent"
		subKey = "SOFTWARE\WOW6432Node\AdventNet\ManageEngine\UESAgent"
	End IF
End If

KillProcess "uesAgentTrayicon.exe"

'To kill smp agent tray icon exe
'===============================

Sub KillProcess(strProcessToKill)
	strComputer = "."

	SET objWMIService = GETOBJECT("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" _
	& strComputer & "\root\cimv2")

	SET colProcess = objWMIService.ExecQuery _
	("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

	count = 0
	FOR EACH objProcess in colProcess
		objProcess.Terminate()
		count = count + 1
	NEXT
End Sub

'Delete Security Manager Plus Agent Directories
'==============================================
Err.Clear

Set objFSO = CreateObject("Scripting.FileSystemObject")

agentInstalledDir = WshShell.RegRead(regkey&"\AgentInstallDir")

'msgbox "ManageEngine Security Manager Plus Agent installed Directory " & agentInstalledDir
'Wscript.Echo  "ManageEngine Security Manager Plus Agent installed Directory : " & agentInstalledDir

if(objFSO.FolderExists(agentInstalledDir) = False) Then
	'msgbox "Security Manager Plus Agent folder already deleted!"
  Wscript.Echo "Security Manager Plus Agent folder already deleted!"
else
	set folder = objFSO.GetFolder(agentInstalledDir)
	folder.Delete

	if(objFSO.FolderExists(agentInstalledDir) = False) Then
	 'msgbox "Security Manager Plus Agent folder deleted successfully"
   Wscript.Echo "UES Agent folder deleted successfully."
	else
	 'msgbox "Problem in deleting Agent folder: " & agentInstalledDir
   Wscript.Echo "Problem in deleting Agent folder : " & agentInstalledDir
   WScript.Echo "Check and Delete the folder manually !!!"
	end if
end if

'msgbox "ManageEngine Security Manager Plus Agent folder cleanup " & Err.Number
Wscript.Echo "ManageEngine Unified Endpoint Security - Agent folder cleanup with ErrorCode : " & Err.Number

'Removing Agent Registry location details
'==========================================

const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

DeleteSubkeys subKey

Sub DeleteSubkeys(strKeyPath)
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys strKeyPath & "\" & strSubkey
        Next
    End If

    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub


'msgbox "ManageEngine Security Manager Plus Agent registry cleanup " & Err.Number





'**********************************************************************************************************************