Solved

Need help with a VBScript to uninstall all versions of Java

Posted on 2008-06-23
8
3,286 Views
Last Modified: 2012-08-29
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

0
Comment
Question by:mcpp661
8 Comments
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21851000
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

0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21851012
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.
0
 
LVL 24

Accepted Solution

by:
purplepomegranite earned 500 total points
ID: 21851114
The attached is the same entirely in VBS.
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"
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")
	'Publisher
	Publisher=WshShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\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
		wscript.echo "Java uninstall command found: " & strUninstall
	end if
end sub

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:mcpp661
ID: 21852255
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.
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21853696
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!
0
 

Author Comment

by:mcpp661
ID: 21860406
It's going to work great. Thanks for your help!
0
 

Expert Comment

by:Nechako91
ID: 23739808
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

0
 

Expert Comment

by:lubin_5200
ID: 38346297
please remove
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

13 Experts available now in Live!

Get 1:1 Help Now