Solved

Need help with a VBScript to uninstall all versions of Java

Posted on 2008-06-23
8
3,274 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

21 Experts available now in Live!

Get 1:1 Help Now