Procedure to uninstall all older versions if the endpoint have multiple JREs

Please use the procedure to uninstall older versions of JREs - if the endpoint has many versions of Java Runtime Environment then the script will remove all old versions of the JRE except the latest one from the endpoint.

def DNDS(rtkey,pK,kA):
    import re
    ln=[]
    lv=[]
    lus=[]
    try:
        oK=_winreg.OpenKey(rtkey,pK,0,kA)
        i=0
        while True:
            try:
                bkey=_winreg.EnumKey(oK,i)
                vkey=os.path.join(pK,bkey)
                oK1=_winreg.OpenKey(rtkey,vkey,0,kA)
                try:
                    DN,bla=_winreg.QueryValueEx(oK1,'DisplayName')
                    DV,bla=_winreg.QueryValueEx(oK1,'DisplayVersion')
                    US,bla=_winreg.QueryValueEx(oK1,'UninstallString')
                    _winreg.CloseKey(oK1)
                    o=re.search('java\s[0-9]+\supdate\s[0-9]+',DN.lower())
                    if o.group():
                        ln.append(DN)
                        lv.append(DV)
                        lus.append(US)
                except:
                    pass
                i += 1
            except:
                break
        _winreg.CloseKey(oK)
        return zip(ln,lv,lus)
    except:
        return zip(ln,lv,lus)

def ecmd(CMD):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    r=OBJ.returncode
    return r

import _winreg
import os
if 'PROGRAMFILES(X86)' in os.environ.keys():
    f=DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ)
    f.extend(DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ))
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft1\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ))
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft1\Windows\CurrentVersion\Uninstall',_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ))
else:
    f=DNDS(_winreg.HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_READ)
    f.extend(DNDS(_winreg.HKEY_CURRENT_USER,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',_winreg.KEY_READ))

if len(f)>0:
    vf=[]
    for i in f:
        vf.append([str(i[0]), [int(k) for k in i[1].split('.')], str(i[2])])
    vf=sorted(vf,key=lambda x:x[1],reverse=True)

    print 'Existing JREs: '
    for i in vf:
        print i[0], '.'.join([str(k) for k in i[1]])

    print '
'
    if len(vf)>1:
        print 'Removed JREs: '
        for i in vf[1:]:
            if ecmd(i[-1]+' /qn')==0:
                print i[0], '.'.join([str(k) for k in i[1]])
else:
    print 'No JRE Installed'

Sample Output:

Script in JSON format:
​​​​​​​

CS5515.png

20170405-CS5515.json (4.46 KB)

Can it be updated to include SDK?

Ran it on a workstation that still have v7 on it:

Existing JREs:
Java 8 Update 121 8.0.1210.13
Java 8 Update 121 (64-bit) 8.0.1210.13

Removed JREs:
Java 8 Update 121 (64-bit) 8.0.1210.13

But it left the Java 7 Update 79 (64-bit) on the PC.
Also, should it be choosing to remove the 64-bit versions from 64-bit machines? If anything, shouldn’t it be leaving that one behind?

Yes, we can update the script for SDK

I will check with package Java 7 Update 79 (64-bit) and let the script work for the same version also.

Sure we will update the script that to leave 64 bit version on 64 bit machine architecture

Inform you once we complete the script.

Thanks for the comments.

Hi @PromptCare,

Please use the script to uninstall older versions of java (JRE and JDK)

The script leaves x64 if you have the latest version of x86 and x64 but uninstalls x86 program

Please refer the link
https://forum.mspconsortium.com/forum/script-library/10582-uninstall-old-javas-jres-and-jdks#post10582

Thank you.