Link to home
Start Free TrialLog in
Avatar of mcpp661
mcpp661

asked on

Need help with a VBScript to uninstall all versions of Java

I need to uninstall all versions of Java from 70 computers in the organization I work for. A friendly Experts-Exchange user pointed me to a forum that contained the code to do this. However, this setup uses a batch file to export a registry key to a file, filter out some unnecessary keys and values to another file, and then call a VBScript to read the filtered file and use the info from that file to search for uninstaller string values to uninstall all versions of Java. However, the code is not working for me and I don't know why. I'm sure it's a syntax issue. Also, I'd like for this to run as just one VBScript. Not a batch file that calls a VBScript. So, can you please help me in 1) converting the batch file code to VBScript code and 2) point out what's wrong with the VBScript? The batch file code is immediately below, the VBScript is attached via the "Attach Code Snippet"

Batch file code to be converted to VBScript code:
REM <start>
@echo off
if not exist c:\temp\. mkdir c:\temp

 REM Export the Uninstall registry keys
start /wait "" REGEDIT /E c:\temp\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall

REM Filter only the {} keys that Java might be in
type c:\temp\registry.tmp | find /i "{" | find /i "}]" > c:\temp\uninstall.tmp

REM Run the Vbscript that uses this file to find Java Sun entries to uninstall
cscript c:\TEMP\java-uninstaller.vbs
REM <end>
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2
 
'Open the exported registry file
 
Set MyFile = fso.OpenTextFile("c:\temp\uninstall.tmp", ForReading)
Do While MyFile.AtEndOfStream <> True
ReadLineTextFile = MyFile.ReadLine
Uninstall = ReadLineTextFile
CLSID = Mid(Uninstall, 73, 38)
On Error Resume Next
 
'DisplayName
DisplayName=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CLSID\DisplayName")
 
'Publisher
Publisher=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CLSID\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
Uninstaller="MsiExec.exe /X"+CLSID+"/QN"
if search1>0 And search2>0 Then Return = WshShell.Run(Uninstaller, 1, TRUE)
Loop

Open in new window

Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

The problem with the VBScript is the attached lines (corrected version attached)
'DisplayName
DisplayName=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName")
wscript.echo DisplayName
'Publisher
Publisher=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher")

Open in new window

Sorry, you can remove the wscript.echo line, I used that to check what was happening.

It is possible to convert it to one script, but the way the batch file works is in fact more efficient than doing that in VBS.  To get VBS to do the same thing is in fact a fair bit of code.  There may be another way of achieving the same result though... I'll see what I can come up with.  In the meantime, please check that the above does solve the problem so far.
ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcpp661
mcpp661

ASKER

Thank you for your posts. I'll try them tomorrow (Tuesday) to see how it goes. The reason I want to do it all in VBScript is that I'm looking for a way to remove Java on several PCs through a GPO via a startup script. I see what you're saying though, as far is it being more efficient through a batch file. I also see where I was going wrong in my syntax. Well, I'll check it at work tomorrow and see how it goes. Again, thank you for your posts.
In this case the VBS is probably more efficient that the batch file now.  It doesn't have to create two temporary files anymore, the VB just iterates through the relevant part of the registry.  I'm not as good with batch as I am with VBS, so I had to work out what the batch was doing before I could convert it!
Avatar of mcpp661

ASKER

It's going to work great. Thanks for your help!
I found that this worked for major versions of 4 and 6, but did not work for version 5. If you make the following changes to the code it will also work for version 5.
'Search for presence of Java and Sun in DisplayName and Publisher
   search1 = Instr(1, DisplayName, "Java", 1)
   search2 = Instr(1, Publisher, "Sun", 1)
   search3 = Instr(1, DisplayName, "J2SE", 1)
 
'Execute removal if there is a match
   if (search1>0 And search2>0) Or (search3>0 And search2>0) Then
	strUninstall="MsiExec.exe /X" & strKey & " /QN"
	WshShell.Run strUninstall, 1, TRUE
	wscript.echo "Java uninstall command found: " & strUninstall
   end if

Open in new window

please remove