I’ve been working on getting a script together that would take all those old outdated versions of Java off of client’s machines without having to go into “Add/Remove Programs” 50 times to do it.
The reason for this, or course, is that older versions of Java contain security vulnerabilities. Some viruses can exploit that vulnerability and thus use Java as the “entry-point” to begin their malicious havoc on your PC. See this article from the Washington Post.
After working off and on today on it, I think I have a solution. Part of the following code I found on a tech blog, so if it looks like you were the author, thanks! You can download the batch file here.
…Or here’s the code to see for yourself:
Rem to run this file and log the output use: "uninstall java.bat">>java_remove.log
Rem @echo off &
cls
Rem List all Installation subkeys from uninstall key.
echo Searching Registry for Java Installs
for /f %%I in (’reg query HKLM\SOFTWARE\microsoft\windows\currentversion\uninstall’) do echo %%I | find “{” > nul && call :All-Installations %%I
echo Search Complete..
goto :NoneFound
:All-Installations
Rem Filter out all but the Sun Installations
for /f “tokens=2*” %%T in (’reg query %1 /v Publisher 2^> nul’) do echo %%U | find “Sun” > nul && call :Sun-Installations %1
goto :EOF
:Sun-Installations
Rem Filter out all but the Sun-Java Installations. Note the tilda + n, which drops all the subkeys from the path
echo %1
for /f “tokens=2*” %%T in (’reg query %1 /v DisplayName 2^> nul’) do echo . Uninstalling - %%U: | find “Java” && call :Sun-Java-Installs %~n1
if errorlevel 1 (
echo Doing further conditional checking on variables.
for /f “tokens=2*” %%T in (’reg query %1 /v DisplayName 2^> nul’) do echo . Uninstalling - %%U: | find “J2SE” && call :Sun-Java-Installs %~n1
)
goto :EOF
:Sun-Java-Installs
Rem Run Uninstaller for the installation
MsiExec.exe /x%1 /qb /quiet /passive /promptrestart
echo . Uninstall Complete, Resuming Search..
goto :EOF
:NoneFound
Rem No Java found to remove from this system
echo No Java found or all versions already removed from this system
goto :EOF
VERY IMPORTANT
If you or your clients are running LOB apps that require legacy versions of Java, this batch file WILL remove them. It removes all versions of Java. The good news is that you can download old versions of Java here: http://java.sun.com/products/archive/. There’s about every outdated version of Java you can dream of there waiting to be downloaded.
Once you have removed all versions of Java, you now need to get the latest version on. You can get that here: http://www.java.com/en/download/manual.jsp.
I hate the auto update feature of Java, so if you want to turn that off, you can go here:
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Update\Policy
Change the EnableJavaUpdate key to 0, and that should do the trick. Someone suggested installing with “JAVAUPDATE=0″, but in my testing that didn’t do anything.
I pushed all this out via Kaseya to my clients. Here are the Kaseya scripts I used:
Remove Java
Script Name: Remove Java
Script Description:
IF Test File
Parameter 1 : C:\ICS\Tools\remove_java.bat
Exists :
THEN
Execute Shell Command
Parameter 1 : C:\ICS\Tools\remove_java.bat>>C:\ICS\Logs\remove_java.log
Parameter 2 : 1
OS Type : 8
Write Script Log Entry
Parameter 1 : All versions of Java removed.
OS Type : 8
ELSE
Write File
Parameter 1 : C:\ICS\Tools\remove_java.bat
Parameter 2 : lpopejoy\Scripts\uninstall java.bat
OS Type : 8
Execute Shell Command
Parameter 1 : C:\ICS\Tools\remove_java.bat>>C:\ICS\Logs\remove_java.log
Parameter 2 : 1
OS Type : 8
Write Script Log Entry
Parameter 1 : All versions of Java removed.
OS Type : 8
Install New Version
Script Name: Install Java
Script Description:
IF True
THEN
Write File
Parameter 1 : C:\ICS\Tools\jre-6u7-windows-i586-p-s.exe
Parameter 2 : lpopejoy\Software\Java\jre-6u7-windows-i586-p-s.exe
OS Type : 0
Execute Shell Command
Parameter 1 : C:\ICS\Tools\jre-6u7-windows-i586-p-s.exe /s /v”/qn IEXPLORER=1 REBOOT=Suppress JAVAUPDATE=0 /L C:\ICS\Logs\JRE6_7Setup.log”
Parameter 2 : 1
OS Type : 0
Set Registry Value
Parameter 1 : HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Update\Policy\EnableJavaUpdate
Parameter 2 : 0
Parameter 3 : REG_DWORD
OS Type : 0
Write Script Log Entry
Parameter 1 : Latest Version of Java installed.
OS Type : 0
ELSE
Do both together
Script Name: Remove Java & Install Latest
Script Description:
IF True
THEN
Execute Script
Parameter 1 : Remove Java (NOTE: Script reference is NOT imported. Correct manually in script editor.
Parameter 2 :
Parameter 3 : 0
OS Type : 8
Execute Script
Parameter 1 : Install Java (NOTE: Script reference is NOT imported. Correct manually in script editor.
Parameter 2 :
Parameter 3 : 0
OS Type : 8
ELSE
All the best!
Luke