Solved

Need help with a VBScript to uninstall all versions of Java

Posted on 2008-06-23
8
3,280 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

23 Experts available now in Live!

Get 1:1 Help Now