Solved

Java uninstall VB Script Help

Posted on 2011-03-17
20
699 Views
Last Modified: 2012-05-11
Im at my wits end. I really need a vb script that will uninstall all Java versions on workstations silently. It needs to be compatible with XP and Windows 7 x64. I use SCCM to deploy packages and I've tried a lot of things, but there are so many java versions on the enterprise it's almost impossible for me to write something that pinpoints each version. Any help would be greatly appreciated.
0
Comment
Question by:obso1337
  • 10
  • 5
  • 5
20 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35163829
0
 

Author Comment

by:obso1337
ID: 35164022
I've looked at this script before. I've added the Windows 7 registry path, but it doesn't seem to uninstall. With the original echo command, it will show the correct uninstall command. It just doesn't execute. Did I put something wrong in here?
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1, ForWriting = 2
strComputer = "."
 
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
' Get array of subkeys under uninstall registry entry
strKeyPath = "SOFTWARE\microsoft\windows\currentversion\uninstall"
strKeyPath = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
For Each subkey In arrSubKeys
    CheckForJava subkey
Next
 
wscript.quit
 
 
sub CheckForJava(strKey)
	On Error Resume Next
	'DisplayName
	DisplayName=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strKey & "\DisplayName")
	DisplayName=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & strKey & "\DisplayName")
	'Publisher
	Publisher=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strKey & "\Publisher")
	Publisher=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & strKey & "\Publisher")
	'Search for presence of Java and Sun in DisplayName and Publisher
	search1 = Instr(1, DisplayName, "Java", 1)
	search2 = Instr(1, Publisher, "Sun", 1)
	 
	'Execute removal if there is a match
	if search1>0 And search2>0 Then
		strUninstall="MsiExec.exe /X" & strKey & " /QN"
		WshShell.Run strUninstall, 1, TRUE
	end if
end sub

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35164316
0
 

Author Comment

by:obso1337
ID: 35164406
Yeah I'll try.
0
 

Author Comment

by:obso1337
ID: 35164476
Well, I ran it from an elevated command prompt on a Windows 7 x64 system with Java 6 update 24 installed and it did nothing.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35164544
Have you tested on XP machine?
0
 

Author Comment

by:obso1337
ID: 35164639
On XP it gives me a line 13 char 1 error "Object not a collection."
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35164918
When you tested in XP have you commented the modified code for win 7?
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35164968
A space is missing after /X, make the necessary changes to the line

            strUninstall="MsiExec.exe /X " & strKey & " /QN"
0
 

Author Comment

by:obso1337
ID: 35165250
Still doesn't work on 7 x64 and XP is giving the same error as before. I made the edit for the space after the x like you suggested.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:Daz_1234
ID: 35165310
Here is a script I wrote a long time ago - it uninstalls all versions of java going back to the original InstallShield ones.  I have used it in our Enterprise of about 3000 computers.

It writes a log in C:\Logs folder, but that is configurable on lines 22 & 23.

Hope this helps,
Daz.


'# D.Collins - 17:02 16/09/2009
'# Uninstalls any and all versions of Java Runtime.
'# Based on a script by 'muaddip' from Appdeploy.com message boards.
'# http://www.appdeploy.com/messageboards/tm.asp?m=29809


Option Explicit

Dim wshShell, fso, strLogFile, ts, strTempDir, strTempISS, strUnString, tsIn, blFound
Dim strUninstLine, CLSID, search5, search6, search7, strJRE1, strDisplayName, strDisplayVersion
Dim strPublisher, strUninstallString, strJREUninstallString, strJREDisplayName
Dim search1, search2, search3, search4, strJREUninstallStringNEW, ret, strUninstCMD
Dim tsISS, strSetupexe, qVal, strComputername

qVal = 0

Set wshShell = CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject") 

strComputername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If Not fso.FolderExists("C:\Logs") Then fso.CreateFolder("C:\Logs")
strLogFile = "C:\Logs\Java_Uninstall_" & strComputername & ".log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.WriteLine Now() & " - Java Runtime(s) uninstallation"
ts.WriteLine String(80, "_") & vbCrlf

'# Generate Registry extracts from 'Uninstall' keys.
PreFlight()

'# Kill Java Processes
KillProc()

strTempDir = wshShell.ExpandEnvironmentStrings("%temp%")
strTempISS = strTempDir & "\iss" 
strUnString = " -s -a /s /f1" 
Set tsIn = fso.OpenTextFile(strTempDir & "\uninstall.tmp", 1) 

If Not fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS) 

blFound = False

Do While Not tsIn.AtEndOfStream
    strUninstLine = tsIn.ReadLine 
    CLSID = Mid(strUninstLine, 73, 38) 
    search5 = Instr(strUninstLine, "JRE 1") 
    search6 = Instr(strUninstLine, "]") 
    If search5 > 0 AND search6 > 0 Then 
        strJRE1 = Replace(Mid(strUninstLine, search5, search6),"]","")   
    End If 

    On Error Resume Next

    strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName") 
    strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion") 
    strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher") 
    strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString") 

    strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString") 
    strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName") 

    On Error Goto 0

    'Search for presence of Java and Sun in DisplayName and Publisher 
    search1 = Instr(1, strDisplayName, "Java", 1) 
    search2 = Instr(1, strPublisher, "Sun", 1) 
    search3 = Instr(1, strDisplayName, "J2SE", 1) 
    search4 = Instr(1, strUninstallString, "setup.exe", 1)
    search7 = InStr(1, strDisplayName, "Development", 1) + InStr(1, strDisplayName, "Java DB", 1)

    If strJREUninstallString <> "" Then
        blFound = True
        '# JRE 1 found
        strJREUninstallStringNEW = Replace(strJREUninstallString," -f"," -s -a /s /f") 
        ts.WriteLine Now() & " - " & strJREDisplayName
        ts.WriteLine Now() & " - Uninstall String sent: " & strJREUninstallStringNEW 
        ret = wshShell.Run(strJREUninstallStringNEW , 0, True)
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618

    ElseIf search7 = 0 And search1 > 0 Or search3 > 0 And search2 > 0 Then
        blFound = True
        strUninstCMD = "msiexec.exe /x " & CLSID & " /norestart /qn"

        If search4 > 0 Then
            '# Old InstallShield setup found
            Set tsISS = fso.OpenTextFile(strTempISS & "\" & CLSID & ".iss", 2, True)
  
            'Create Response file for any Java Version 
            tsISS.WriteLine "[InstallShield Silent]" 
            tsISS.WriteLine "Version=v6.00.000" 
            tsISS.WriteLine "File=Response File" 
            tsISS.WriteLine "[File Transfer]" 
            tsISS.WriteLine "OverwrittenReadOnly=NoToAll" 
            tsISS.WriteLine "[" & CLSID & "-DlgOrder]" 
            tsISS.WriteLine "Dlg0=" & CLSID & "-SprintfBox-0" 
            tsISS.WriteLine "Count=2" 
            tsISS.WriteLine "Dlg1=" & CLSID & "-File Transfer" 
            tsISS.WriteLine "[" & CLSID & "-SprintfBox-0]" 
            tsISS.WriteLine "Result=1" 
            tsISS.WriteLine "[Application]" 
            tsISS.WriteLine "Name=Java 2 Runtime Environment, SE v1.4.0_01"
            tsISS.WriteLine "Version=1.4.0_01"
            tsISS.WriteLine "Company=JavaSoft"
            tsISS.WriteLine "Lang=0009"
            tsISS.WriteLine "[" & CLSID & "-File Transfer]"
            tsISS.WriteLine "SharedFile=YesToAll"
            tsISS.Close

            strSetupexe = Left(strUninstallString, search4 + 9) 
            strUninstCMD =  strSetupexe & strUnString & Chr(34) & strTempISS & "\" & CLSID & ".iss" & Chr(34) 
        End If

        ts.WriteLine Now() & " - " & strDisplayName & "    - Version: " & strDisplayVersion
        ts.WriteLine Now() & " - Uninstall String sent: " & strUninstCMD
        ret = wshShell.Run(strUninstCMD , 0, True) 
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618
    End If 

Loop

tsIn.Close

If Not blFound Then
    ts.WriteLine Now() & " - No Java Runtime versions found installed."
    qVal = 99
End If

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.Close
fso.DeleteFolder(strTempISS)
fso.DeleteFile(strTempDir & "\uninstall.tmp")

WScript.Quit(qVal)

Sub PreFlight()
    '# Creates temp files containing extracts from registry 'Uninstall' keys.
    Dim wshShell, fso, sTemp
    Set wshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    sTemp = wshShell.ExpandEnvironmentStrings("%temp%")
    wshShell.Run "REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall", 0, True
    wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""{"" | find /i ""}]"" > %temp%\uninstall.tmp ", 0, True
    wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""JRE 1"" >> %temp%\uninstall.tmp ", 0, True
    If Not fso.FileExists(sTemp & "\uninstall.tmp") Then
        ts.WriteLine Now() & " - No input - %temp%\uninstall.tmp Reg extract not created."
        ts.WriteLine String(80, "_") 
        ts.WriteLine String(80, "¯") 
        ts.Close
        WScript.Quit(1)
    End If
End Sub

Sub KillProc()
    '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run "Taskkill /F /IM jusched.exe /T", 0, True
    wshShell.Run "Taskkill /F /IM jqs.exe /T", 0, True
End Sub

Open in new window

0
 

Author Comment

by:obso1337
ID: 35165553
I've edited the script to add Win 7 functionality. The log says no java installation found though. There is java 6 update 24 installed. On XP it gave an error line 151 char 5 "unable to wait for process." Talk about crazy.
'# D.Collins - 17:02 16/09/2009
'# Uninstalls any and all versions of Java Runtime.
'# Based on a script by 'muaddip' from Appdeploy.com message boards.
'# http://www.appdeploy.com/messageboards/tm.asp?m=29809


Option Explicit

Dim wshShell, fso, strLogFile, ts, strTempDir, strTempISS, strUnString, tsIn, blFound
Dim strUninstLine, CLSID, search5, search6, search7, strJRE1, strDisplayName, strDisplayVersion
Dim strPublisher, strUninstallString, strJREUninstallString, strJREDisplayName
Dim search1, search2, search3, search4, strJREUninstallStringNEW, ret, strUninstCMD
Dim tsISS, strSetupexe, qVal, strComputername

qVal = 0

Set wshShell = CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject") 

strComputername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If Not fso.FolderExists("C:\Logs") Then fso.CreateFolder("C:\Logs")
strLogFile = "C:\Logs\Java_Uninstall_" & strComputername & ".log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.WriteLine Now() & " - Java Runtime(s) uninstallation"
ts.WriteLine String(80, "_") & vbCrlf

'# Generate Registry extracts from 'Uninstall' keys.
PreFlight()

'# Kill Java Processes
KillProc()

strTempDir = wshShell.ExpandEnvironmentStrings("%temp%")
strTempISS = strTempDir & "\iss" 
strUnString = " -s -a /s /f1" 
Set tsIn = fso.OpenTextFile(strTempDir & "\uninstall.tmp", 1) 

If Not fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS) 

blFound = False

Do While Not tsIn.AtEndOfStream
    strUninstLine = tsIn.ReadLine 
    CLSID = Mid(strUninstLine, 73, 38) 
    search5 = Instr(strUninstLine, "JRE 1") 
    search6 = Instr(strUninstLine, "]") 
    If search5 > 0 AND search6 > 0 Then 
        strJRE1 = Replace(Mid(strUninstLine, search5, search6),"]","")   
    End If 

    On Error Resume Next

    strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName")
    strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName")	
    strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion")
    strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion")	
    strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher")
    strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher")	
    strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString") 
    strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString")
	
    strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString")
    strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString")	
    strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName")
    strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName")	

    On Error Goto 0

    'Search for presence of Java and Sun in DisplayName and Publisher 
    search1 = Instr(1, strDisplayName, "Java", 1) 
    search2 = Instr(1, strPublisher, "Sun", 1) 
    search3 = Instr(1, strDisplayName, "J2SE", 1) 
    search4 = Instr(1, strUninstallString, "setup.exe", 1)
    search7 = InStr(1, strDisplayName, "Development", 1) + InStr(1, strDisplayName, "Java DB", 1)

    If strJREUninstallString <> "" Then
        blFound = True
        '# JRE 1 found
        strJREUninstallStringNEW = Replace(strJREUninstallString," -f"," -s -a /s /f") 
        ts.WriteLine Now() & " - " & strJREDisplayName
        ts.WriteLine Now() & " - Uninstall String sent: " & strJREUninstallStringNEW 
        ret = wshShell.Run(strJREUninstallStringNEW , 0, True)
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618

    ElseIf search7 = 0 And search1 > 0 Or search3 > 0 And search2 > 0 Then
        blFound = True
        strUninstCMD = "msiexec.exe /x " & CLSID & " /norestart /qn"

        If search4 > 0 Then
            '# Old InstallShield setup found
            Set tsISS = fso.OpenTextFile(strTempISS & "\" & CLSID & ".iss", 2, True)
  
            'Create Response file for any Java Version 
            tsISS.WriteLine "[InstallShield Silent]" 
            tsISS.WriteLine "Version=v6.00.000" 
            tsISS.WriteLine "File=Response File" 
            tsISS.WriteLine "[File Transfer]" 
            tsISS.WriteLine "OverwrittenReadOnly=NoToAll" 
            tsISS.WriteLine "[" & CLSID & "-DlgOrder]" 
            tsISS.WriteLine "Dlg0=" & CLSID & "-SprintfBox-0" 
            tsISS.WriteLine "Count=2" 
            tsISS.WriteLine "Dlg1=" & CLSID & "-File Transfer" 
            tsISS.WriteLine "[" & CLSID & "-SprintfBox-0]" 
            tsISS.WriteLine "Result=1" 
            tsISS.WriteLine "[Application]" 
            tsISS.WriteLine "Name=Java 2 Runtime Environment, SE v1.4.0_01"
            tsISS.WriteLine "Version=1.4.0_01"
            tsISS.WriteLine "Company=JavaSoft"
            tsISS.WriteLine "Lang=0009"
            tsISS.WriteLine "[" & CLSID & "-File Transfer]"
            tsISS.WriteLine "SharedFile=YesToAll"
            tsISS.Close

            strSetupexe = Left(strUninstallString, search4 + 9) 
            strUninstCMD =  strSetupexe & strUnString & Chr(34) & strTempISS & "\" & CLSID & ".iss" & Chr(34) 
        End If

        ts.WriteLine Now() & " - " & strDisplayName & "    - Version: " & strDisplayVersion
        ts.WriteLine Now() & " - Uninstall String sent: " & strUninstCMD
        ret = wshShell.Run(strUninstCMD , 0, True) 
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618
    End If 

Loop

tsIn.Close

If Not blFound Then
    ts.WriteLine Now() & " - No Java Runtime versions found installed."
    qVal = 99
End If

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.Close
fso.DeleteFolder(strTempISS)
fso.DeleteFile(strTempDir & "\uninstall.tmp")

WScript.Quit(qVal)

Sub PreFlight()
    '# Creates temp files containing extracts from registry 'Uninstall' keys.
    Dim wshShell, fso, sTemp
    Set wshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    sTemp = wshShell.ExpandEnvironmentStrings("%temp%")
    wshShell.Run "REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall", 0, True
	wshShell.Run "REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\microsoft\windows\currentversion\uninstall", 0, True
    wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""{"" | find /i ""}]"" > %temp%\uninstall.tmp ", 0, True
    wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""JRE 1"" >> %temp%\uninstall.tmp ", 0, True
    If Not fso.FileExists(sTemp & "\uninstall.tmp") Then
        ts.WriteLine Now() & " - No input - %temp%\uninstall.tmp Reg extract not created."
        ts.WriteLine String(80, "_") 
        ts.WriteLine String(80, "¯") 
        ts.Close
        WScript.Quit(1)
    End If
End Sub

Sub KillProc()
    '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run "Taskkill /F /IM jusched.exe /T", 0, True
    wshShell.Run "Taskkill /F /IM jqs.exe /T", 0, True
End Sub

Open in new window

0
 
LVL 12

Accepted Solution

by:
Daz_1234 earned 500 total points
ID: 35165647
Is that line 151 in the script I posted?  I've never seen that happen.

You should not need the script for Windows 7.  Since JRE version 6 update 11, they always upgrade the previous version and do not leave old versions lying around.

I've amended the script slightly, not just calling 'cmd' but using the proper %ComSpec% environment variable.

Regards,
Darren.
'# D.Collins - 17:02 16/09/2009
'# Uninstalls any and all versions of Java Runtime.
'# Based on a script by 'muaddip' from Appdeploy.com message boards.
'# http://www.appdeploy.com/messageboards/tm.asp?m=29809


Option Explicit

Dim wshShell, fso, strLogFile, ts, strTempDir, strTempISS, strUnString, tsIn, blFound
Dim strUninstLine, CLSID, search5, search6, search7, strJRE1, strDisplayName, strDisplayVersion
Dim strPublisher, strUninstallString, strJREUninstallString, strJREDisplayName
Dim search1, search2, search3, search4, strJREUninstallStringNEW, ret, strUninstCMD
Dim tsISS, strSetupexe, qVal, strComputername

qVal = 0

Set wshShell = CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject") 

strComputername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If Not fso.FolderExists("C:\Logs") Then fso.CreateFolder("C:\Logs")
strLogFile = "C:\Logs\Java_Uninstall_" & strComputername & ".log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.WriteLine Now() & " - Java Runtime(s) uninstallation"
ts.WriteLine String(80, "_") & vbCrlf

'# Generate Registry extracts from 'Uninstall' keys.
PreFlight()

'# Kill Java Processes
KillProc()

strTempDir = wshShell.ExpandEnvironmentStrings("%temp%")
strTempISS = strTempDir & "\iss" 
strUnString = " -s -a /s /f1" 
Set tsIn = fso.OpenTextFile(strTempDir & "\uninstall.tmp", 1) 

If Not fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS) 

blFound = False

Do While Not tsIn.AtEndOfStream
    strUninstLine = tsIn.ReadLine 
    CLSID = Mid(strUninstLine, 73, 38) 
    search5 = Instr(strUninstLine, "JRE 1") 
    search6 = Instr(strUninstLine, "]") 
    If search5 > 0 AND search6 > 0 Then 
        strJRE1 = Replace(Mid(strUninstLine, search5, search6),"]","")   
    End If 

    On Error Resume Next

    strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName") 
    strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion") 
    strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher") 
    strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString") 

    strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString") 
    strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName") 

    On Error Goto 0

    'Search for presence of Java and Sun in DisplayName and Publisher 
    search1 = Instr(1, strDisplayName, "Java", 1) 
    search2 = Instr(1, strPublisher, "Sun", 1) 
    search3 = Instr(1, strDisplayName, "J2SE", 1) 
    search4 = Instr(1, strUninstallString, "setup.exe", 1)
    search7 = InStr(1, strDisplayName, "Development", 1) + InStr(1, strDisplayName, "Java DB", 1)

    If strJREUninstallString <> "" Then
        blFound = True
        '# JRE 1 found
        strJREUninstallStringNEW = Replace(strJREUninstallString," -f"," -s -a /s /f") 
        ts.WriteLine Now() & " - " & strJREDisplayName
        ts.WriteLine Now() & " - Uninstall String sent: " & strJREUninstallStringNEW 
        ret = wshShell.Run(strJREUninstallStringNEW , 0, True)
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618

    ElseIf search7 = 0 And search1 > 0 Or search3 > 0 And search2 > 0 Then
        blFound = True
        strUninstCMD = "msiexec.exe /x " & CLSID & " /norestart /qn"

        If search4 > 0 Then
            '# Old InstallShield setup found
            Set tsISS = fso.OpenTextFile(strTempISS & "\" & CLSID & ".iss", 2, True)
  
            'Create Response file for any Java Version 
            tsISS.WriteLine "[InstallShield Silent]" 
            tsISS.WriteLine "Version=v6.00.000" 
            tsISS.WriteLine "File=Response File" 
            tsISS.WriteLine "[File Transfer]" 
            tsISS.WriteLine "OverwrittenReadOnly=NoToAll" 
            tsISS.WriteLine "[" & CLSID & "-DlgOrder]" 
            tsISS.WriteLine "Dlg0=" & CLSID & "-SprintfBox-0" 
            tsISS.WriteLine "Count=2" 
            tsISS.WriteLine "Dlg1=" & CLSID & "-File Transfer" 
            tsISS.WriteLine "[" & CLSID & "-SprintfBox-0]" 
            tsISS.WriteLine "Result=1" 
            tsISS.WriteLine "[Application]" 
            tsISS.WriteLine "Name=Java 2 Runtime Environment, SE v1.4.0_01"
            tsISS.WriteLine "Version=1.4.0_01"
            tsISS.WriteLine "Company=JavaSoft"
            tsISS.WriteLine "Lang=0009"
            tsISS.WriteLine "[" & CLSID & "-File Transfer]"
            tsISS.WriteLine "SharedFile=YesToAll"
            tsISS.Close

            strSetupexe = Left(strUninstallString, search4 + 9) 
            strUninstCMD =  strSetupexe & strUnString & Chr(34) & strTempISS & "\" & CLSID & ".iss" & Chr(34) 
        End If

        ts.WriteLine Now() & " - " & strDisplayName & "    - Version: " & strDisplayVersion
        ts.WriteLine Now() & " - Uninstall String sent: " & strUninstCMD
        ret = wshShell.Run(strUninstCMD , 0, True) 
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 And ret <> 1605 And ret <> 1618 Then qVal = 1
        If ret = 1618 Then qVal = 1618
    End If 

Loop

tsIn.Close

If Not blFound Then
    ts.WriteLine Now() & " - No Java Runtime versions found installed."
    qVal = 99
End If

ts.WriteLine String(80, "_") 
ts.WriteLine String(80, "¯") 
ts.Close
fso.DeleteFolder(strTempISS)
fso.DeleteFile(strTempDir & "\uninstall.tmp")

WScript.Quit(qVal)

Sub PreFlight()
    '# Creates temp files containing extracts from registry 'Uninstall' keys.
    Dim wshShell, fso, sTemp, strCMD
    Set wshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    strCMD = wshShell.ExpandEnvironmentStrings("%ComSpec%")
    sTemp  = wshShell.ExpandEnvironmentStrings("%temp%")
    wshShell.Run "REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall", 0, True
    wshShell.Run strCMD & " /c type %temp%\registry.tmp | find /i ""{"" | find /i ""}]"" > %temp%\uninstall.tmp ", 0, True
    wshShell.Run strCMD & " /c type %temp%\registry.tmp | find /i ""JRE 1"" >> %temp%\uninstall.tmp ", 0, True
    If Not fso.FileExists(sTemp & "\uninstall.tmp") Then
        ts.WriteLine Now() & " - No input - %temp%\uninstall.tmp Reg extract not created."
        ts.WriteLine String(80, "_") 
        ts.WriteLine String(80, "¯") 
        ts.Close
        WScript.Quit(1)
    End If
End Sub

Sub KillProc()
    '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run "Taskkill /F /IM jusched.exe /T", 0, True
    wshShell.Run "Taskkill /F /IM jqs.exe /T", 0, True
End Sub

Open in new window

0
 

Author Comment

by:obso1337
ID: 35165769
I've tried your amended script, but in the logs on the 7 x64 machine I get this:

3/18/2011 10:15:06 AM - Java Runtime(s) uninstallation
________________________________________________________________________________

3/18/2011 10:15:15 AM - No Java Runtime versions found installed.

XP seems to work.
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35165889

I'm sorry I should have said - my amended script will not work with x64.  

But as I said, since JRE 6 update 11, the JRE install has been 'nice' and upgraded the previous one, without leaving old versions lying around.  Bearing that in mind, there should only be one version installed on a Windows 7 computer, and installing the latest version should upgrade whatever is there.

So unless you have been installing JRE's from 2008 and earlier, I don't understand why you would need to run the script on Windows 7.

Regards,
Daz.
0
 

Author Comment

by:obso1337
ID: 35166599
Currently we are deploying java with the msi and it is not properly upgrading. So it requires us to uninstall the current version before installing the newer version. I've tried deploying the exe, but it fails the windows 7 systems.
0
 

Author Closing Comment

by:obso1337
ID: 35166793
Wasn't exactly what I needed, but it will work.
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35167339
You was a bit quick there to close this off.  I have just created a script (below) that uses Windows Installer to find the Java installation and then runs an uninstall.

Please try this on Windows 7 - it is not silent and displays a pop up, but that is just for testing.  If it works then I can tweak it for silent + logging.

Regards,
Daz.
StrResults = fUninstallJRE()

MsgBox strResults


Function fUninstallJRE()
    Dim wshShell, objInstaller, colProducts, oProd, strName, temp, ret, sResults
    Set wshShell = CreateObject("WScript.Shell")
    Set objInstaller = CreateObject("WindowsInstaller.Installer")
    Set colProducts = objInstaller.Products
    For Each oProd In colProducts
        strName = objInstaller.ProductInfo (oProd, "ProductName")
        If LCase(Left(strName, 10)) = "java(tm) 6" Then
            ret = wshShell.run("msiexec /x " & oProd & " /qb", 1, True)
            sResults = sResults & strName & " - Uninstall" & vbTab & "Return: " & ret & vbCrlf
        End If
    Next
    fUninstallJRE = sResults
End Function

Open in new window

0
 

Author Comment

by:obso1337
ID: 35167496
That script works on both OS's. I just need it to be silent. Thanks for your hard work.
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35167561
Here you go.

If you find it is not working, test it by commenting out the On Error Resume Next line to see if it gives an error.

It logs to C:\Logs\Java_Uninstall.log

Daz.
Option Explicit

On Error Resume Next

Dim strResults, fso, ts, strLogFile
Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FolderExists("C:\Logs") Then fso.CreateFolder("C:\Logs")
strLogFile = "C:\Logs\Java_Uninstall.log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLine Now() & " - Start of " & WScript.ScriptFullName
strResults = fUninstallJRE()

ts.WriteLine Now() & " - Script Complete"

Function fUninstallJRE()
    Dim wshShell, objInstaller, colProducts, oProd, strName, strCmd, ret, sResults
    Set wshShell = CreateObject("WScript.Shell")
    Set objInstaller = CreateObject("WindowsInstaller.Installer")
    Set colProducts = objInstaller.Products
    For Each oProd In colProducts
        strName = objInstaller.ProductInfo (oProd, "ProductName")
        If LCase(Left(strName, 10)) = "java(tm) 6" Then
            strCmd = "msiexec /x " & oProd & " /qn"
            ts.WriteLine Now() & " - Uninstalling '" & strName & "' calling command '" & strCmd & "' ..."
            ret = wshShell.run(strCmd, 0, True)
            ts.WriteLine Now() & " - Return: " & ret
            sResults = sResults & strName & " - Uninstall" & vbTab & "Return: " & ret & vbCrlf
        End If
    Next
    fUninstallJRE = sResults
End Function

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now