'# D.Collins - 14:11 03/10/2012 '# Uninstall all JRE versions that are Windows-Installer-based. '# D.Collins - 11:33 29/01/2014 - amended to quit with NFA on 1602 (JRE in use) Option Explicit Dim wshShell, fso, ret, strLog Set wshShell = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") strLog = wshShell.ExpandEnvironmentStrings("%TEMP%\JRE_Uninst_log.log") '# KILL JQS.EXE, which can fail the uninstall. wshShell.Run "TaskKill.exe /F /IM jqs.exe", 0, True '# Start the uninstall sub-routine: UninstallAllJREWI(True) Sub UninstallAllJREWI(blForce) '# Uninstalls all Windows-Installer-based JRE installations Dim wshShell, oInstaller, colProducts, oProductCode, sProductName, sCmd, ret Set wshShell = CreateObject("WScript.Shell") Set oInstaller = CreateObject("WindowsInstaller.Installer") Set colProducts = oInstaller.Products For Each oProductCode In colProducts sProductName = oInstaller.ProductInfo (oProductCode, "ProductName") If fRegExpValidate("^java(\(TM\)|)\s\d+\supdate\s\d+$", sProductName) = True Then '# MsgBox oProductCode,, sProductName sCmd = "msiexec /x " & oProductCode & " /norestart /qb-" WLog Now() & " - JRE installation found (" & sProductName & ") - attempting uninstall command: " & sCmd ret = wshShell.Run(sCmd, 0, true) WLog Now() & " - Return: " & ret If ret = 1602 Then '# Exit straight away - JRE is probably in use (as user can't see cancel button) WLog Now() & " - 1602 returned on uninstall - JRE is probably in use - exiting script and returning 1602" WScript.Quit 1602 End If End If Next End Sub Function fRegExpValidate(sPattern, sTst) Dim regEx, retVal Set regEx = New RegExp ' Create a regular expression. regEx.Pattern = sPattern ' Set pattern. regEx.IgnoreCase = True ' Set case insensitivity. regEx.Global = True ' Set global applicability. retVal = regEx.Test(sTst) ' Execute the search test. If retVal Then fRegExpValidate = True Else fRegExpValidate = False End If End Function Sub WLog(sTxt) Dim ts, sErrTxt On Error Resume Next Set ts = fso.OpenTextFile(strLog, 8, True) ts.WriteLine sTxt ts.Close If Err.Number Then If Not fso.FileExists(strLog) Then sErrTxt = WScript.ScriptName & " - ERROR: Unable to write to log file '" & strLog & "' - Error " & Err.Number & " - " & Err.Description & vbCrlf & vbCrlf & "Log Entry: " & sTxt wshShell.LogEvent 1, sErrTxt WScript.Quit 114031 End If WScript.Sleep 100 Wlog(sTxt) End If On Error Goto 0 Set ts = Nothing End Sub