Guest User

[BATCH] Java Runtime Nuker v1.5.0

a guest
Jul 24th, 2013
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. :: Purpose:       1. Nuke ALL versions of JavaFX and the Java Runtime, series 3 through 7, x86 and x64
  2. ::                2. Leaves Java Development Kit installations intact
  3. ::                3. Reinstalls the latest JRE (if you want it to)
  4. ::                4. Puts the lotion on its skin.
  5. :: Requirements:  local administrative rights; WMI
  6. :: Author:        vocatus on reddit.com/r/sysadmin and /r/usefulscripts
  7. ::                additional thanks to:
  8. ::                 - reddit.com/user/sdjason         : JRE reinstall functionality; selective process killing; et al
  9. ::                 - reddit.com/user/MrYiff          : bug fix related to OS_VERSION variable
  10. ::                 - reddit.com/user/cannibalkitteh  : additional registry & file cleaning locations
  11. ::                 - forums.oracle.com/people/mattmn : a lot of stuff from his Java removal script
  12. :: History:       1.5.0 + FEATURE:      Added ability to choose whether or not to kill running Java processes before executing,
  13. ::                                      along with a variable to specify an exit code to use                   (sdjason)
  14. ::                      + FEATURE:      Added ability to selectively reinstall x64 and x86 versions of the JRE (sdjason)
  15. ::                      * IMPROVEMENT:  Converted JRE 3 uninstaller section to a FOR loop
  16. ::                      * IMPROVEMENT:  Converted many commands into FOR loops with test cases to check if they should run or not (sdjason)
  17. ::                      * IMPROVEMENT:  File deletion commands now aren't run if their target doesn't exist.
  18. ::                                      This should reduce unecessary errors in the console and log.     (sdjason)
  19. ::                      / FIX:          Fixed incorrect search string in XP version of Java installer cache purge
  20. ::                1.4.1 / FIX:          Re-enabled "echo off" statement at beginning of script
  21. ::                      / FIX:          Fixed empty OS_VERSION variable on Vista/7/2008/8/2012           (MrYiff)
  22. ::                1.4   + FEATURE:      Added check to see if we're on Windows XP, to run different code for certain sections
  23. ::                      + FEATURE:      Added comprehensive WMI repair if it's broken
  24. ::                      + FEATURE:      Added XP versions of a lot of the code
  25. ::                1.3   + FEATURE:      Added variables to reinstall Java after cleanup (off by default) (sdjason)
  26. ::                      + FILE CLEANUP: Added C:\Users*\AppData\LocalLow\Sun\Java\jre*                   (cannibalkitteh)
  27. ::                      + FILE CLEANUP: Added C:\Users*\AppData\LocalLow\Sun\Java\AU                     (cannibalkitteh)
  28. ::                1.2   + COMMENTS:     Improved a lot of commenting
  29. ::                      + UNINSTALLER:  Added WMIC wildcard-matching to catch all JRE GUIDs, including future revisions
  30. ::                      + FILE CLEANUP: Major overhaul to section                                        (mattm)
  31. ::                      + REGISTRY:     Added additional locations:                                      (cannibalkitteh)
  32. ::                        - HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  33. ::                        - HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
  34. ::                      + PREP:         Added Chrome to the list of browsers to kill before starting
  35. ::                      + PREP:         Added /T flag (terminate child processes) to all browser and Java kill lines
  36. ::                      * LOGGING:      Minor improvements
  37. ::                1.1   + Overhaul of functionality and logging
  38. ::                1.0     Initial write
  39.  
  40. :: Prep
  41. @echo off
  42. set VERSION=1.5.0
  43. set UPDATED=2013-07-23
  44. title Java Runtime Nuker v%VERSION% (%UPDATED%)
  45.  
  46. :::::::::::::::
  47. :: VARIABLES :: -- set these if you'd like. The defaults should work fine though
  48. :::::::::::::::
  49. :: Rules for variables:
  50. ::  * NO quotes!                       (bad:  "c:\directory\path"       )
  51. ::  * NO trailing slashes on the path! (bad:   c:\directory\            )
  52. ::  * Spaces are okay                  (okay:  c:\my folder\with spaces )
  53. ::  * Network paths are okay           (okay:  \\server\share name      )
  54. ::                                     (       \\172.16.1.5\share name  )
  55.  
  56. :: Log settings
  57. set LOGPATH=%SystemDrive%\Logs
  58. set LOGFILE=%COMPUTERNAME%_java_runtime_removal.log
  59.  
  60. :: Force-close Java processes? Recommend leaving this set to 'yes' unless you
  61. :: specifically want to abort the script if the target machine is currently using Java.
  62. :: If you change this to 'no', the script will exit with an error code if it finds any running processes.
  63. set FORCE_CLOSE_PROCESSES=yes
  64. :: Exit code to use when FORCE_CLOSE_PROCESSES is "no" and a running Java process is detected
  65. set FORCE_CLOSE_PROCESSES_EXIT_CODE=1618
  66.  
  67. :: Java re-install. Do you want to reinstall Java afterwards?
  68. :: Change either of these to 'yes' if you want to reinstall Java after cleanup.
  69. :: If you do, make sure to set the location, file names and arguments below!
  70. set REINSTALL_JAVA_x64=no
  71. set REINSTALL_JAVA_x86=no
  72.  
  73. :: The JRE installer must be in a place the script can find it (e.g. network path, same directory, etc)
  74. :: JRE 64-bit reinstaller
  75. set JAVA_LOCATION_x64=%~dp0
  76. set JAVA_BINARY_x64=jre-7u25-windows-x64.exe
  77. set JAVA_ARGUMENTS_x64=/s /v"ADDLOCAL=ALL IEXPLORER=1 MOZILLA=1 JAVAUPDATE=0 REBOOT=suppress" /qn
  78.  
  79. :: JRE 32-bit reinstaller
  80. set JAVA_LOCATION_x86=%~dp0
  81. set JAVA_BINARY_x86=jre-7u25-windows-x86.exe
  82. set JAVA_ARGUMENTS_x86=/s /v"ADDLOCAL=ALL IEXPLORER=1 MOZILLA=1 JAVAUPDATE=0 REBOOT=suppress" /qn
  83.  
  84.  
  85. :: =============================================================================================== ::
  86. :: ======  Think of everything below this line like a feral badger: Look, but Do Not Touch  ====== ::
  87. :: =============================================================================================== ::
  88.  
  89.  
  90. :: Check if we're on XP. This affects some commands later, because XP uses slightly
  91. :: different binaries for reg.exe and various other Windows utilities
  92. set OS_VERSION=OTHER
  93. ver | find /i "XP" >NUL
  94. IF %ERRORLEVEL%==0 set OS_VERSION=XP
  95.  
  96. :: Force WMIC location in case the system PATH is messed up
  97. set WMIC=%WINDIR%\system32\wbem\wmic.exe
  98.  
  99. :: Create the log directory if it doesn't exist
  100. if not exist %LOGPATH% mkdir %LOGPATH%
  101. if exist "%LOGPATH%\%LOGFILE%" del "%LOGPATH%\%LOGFILE%"
  102.  
  103. :: Get the date into a format we can use (ISO standard date format)
  104. set CUR_DATE=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
  105.  
  106.  
  107. :::::::::::::::::::::
  108. :: PREP AND CHECKS ::
  109. :::::::::::::::::::::
  110. echo.
  111. echo  JAVA RUNTIME NUKER
  112. echo  v%VERSION%, updated %UPDATED%
  113. if %OS_VERSION%==XP echo. && echo  ! Windows XP detected, using alternate command set to compensate.
  114. echo.
  115. echo %CUR_DATE% %TIME%   Beginning removal of Java Runtime Environments (series 3-7, x86 and x64) and JavaFX...>> "%LOGPATH%\%LOGFILE%"
  116. echo %CUR_DATE% %TIME%   Beginning removal of Java Runtime Environments (series 3-7, x86 and x64) and JavaFX...
  117.  
  118. :: Do a quick check to make sure WMI is working, and if not, repair it
  119. wmic timezone >NUL
  120. if not %ERRORLEVEL%==0 (
  121.     echo %CUR_DATE% %TIME% ! WMI appears to be broken. Running WMI repair. This might take a minute, please be patient...>> "%LOGPATH%\%LOGFILE%"
  122.     echo %CUR_DATE% %TIME% ! WMI appears to be broken. Running WMI repair. This might take a minute, please be patient...
  123.     net stop winmgmt
  124.     pushd %WINDIR%\system32\wbem
  125.     for %%i in (*.dll) do RegSvr32 -s %%i
  126.     :: Kill this random window that pops up
  127.     tskill wbemtest /a 2>NUL
  128.     scrcons.exe /RegServer
  129.     unsecapp.exe /RegServer
  130.     start "" wbemtest.exe /RegServer
  131.     tskill wbemtest /a 2>NUL
  132.     tskill wbemtest /a 2>NUL
  133.     winmgmt.exe /RegServer
  134.     wmiadap.exe /RegServer
  135.     wmiapsrv.exe /RegServer
  136.     wmiprvse.exe /RegServer
  137.     net start winmgmt
  138.     popd
  139. )
  140.  
  141.  
  142. :::::::::::::::::::::::::::
  143. :: FORCE-CLOSE PROCESSES :: -- Do we want to kill Java before running? If so, this is where it happens
  144. :::::::::::::::::::::::::::
  145. if %FORCE_CLOSE_PROCESSES%==yes (
  146.     :: Kill all browsers and running Java instances
  147.     echo %CUR_DATE% %TIME%   Looking for and closing all running browsers and Java instances...>> "%LOGPATH%\%LOGFILE%"
  148.     echo %CUR_DATE% %TIME%   Looking for and closing all running browsers and Java instances...
  149.     if %OS_VERSION%==XP (
  150.         :: XP version of the task killer
  151.         :: this loop contains the processes we should kill
  152.         echo.
  153.         FOR %%i IN (java,javaw,javaws,jqs,jusched,iexplore,iexplorer,firefox,chrome,palemoon) DO (
  154.             echo Searching for %%i.exe...
  155.             tskill /a /v %%i >> "%LOGPATH%\%LOGFILE%" 2>NUL
  156.         )
  157.         echo.
  158.     ) else (
  159.         :: 7/8/2008/2008R2/2012/etc version of the task killer
  160.         :: this loop contains the processes we should kill
  161.         echo.
  162.         FOR %%i IN (java,javaw,javaws,jqs,jusched,iexplore,iexplorer,firefox,chrome,palemoon) DO (
  163.             echo Searching for %%i.exe...
  164.             taskkill /f /im %%i.exe /T >> "%LOGPATH%\%LOGFILE%" 2>NUL
  165.         )
  166.         echo.
  167.     )
  168. )
  169.  
  170. :: If we DON'T want to force-close Java, then check for possible running Java processes and abort the script if we find any
  171. if %FORCE_CLOSE_PROCESSES%==no (
  172.     echo %CUR_DATE% %TIME%   Variable FORCE_CLOSE_PROCESSES is set to '%FORCE_CLOSE_PROCESSES%'. Checking for running processes before execution.>> "%LOGPATH%\%LOGFILE%"
  173.     echo %CUR_DATE% %TIME%   Variable FORCE_CLOSE_PROCESSES is set to '%FORCE_CLOSE_PROCESSES%'. Checking for running processes before execution.
  174.  
  175.     :: Don't ask...
  176.     :: Okay so basically we loop through this list of processes, and for each one we dump the result of the search in the '%%a' variable.
  177.     :: Then we check that variable, and if it's not null (e.g. FIND.exe found something) we abort the script, returning the exit code
  178.     :: specified at the beginning of the script. Normally you'd use ERRORLEVEL for this, but because it is very flaky (it doesn't
  179.     :: always get set, even when it should) we instead resort to using this method of dumping the results in a variable and checking it.
  180.     FOR %%i IN (java,javaw,javaws,jqs,jusched,iexplore,iexplorer,firefox,chrome,palemoon) DO (
  181.         echo %CUR_DATE% %TIME%   Searching for %%i.exe...
  182.         for /f "delims=" %%a in ('tasklist ^| find /i "%%i"') do (
  183.             if not [%%a]==[] (
  184.                 echo %CUR_DATE% %TIME% ! ERROR: Process '%%i' is currently running, aborting.>> "%LOGPATH%\%LOGFILE%"
  185.                 echo %CUR_DATE% %TIME% ! ERROR: Process '%%i' is currently running, aborting.
  186.                 exit /b %FORCE_CLOSE_PROCESSES_EXIT_CODE%
  187.             )
  188.         )
  189.     )
  190.     :: If we made it this far, we didn't find anything, so we can go ahead
  191.     echo %CUR_DATE% %TIME%   All clear, no running processes found. Going ahead with removal...>> "%LOGPATH%\%LOGFILE%"
  192.     echo %CUR_DATE% %TIME%   All clear, no running processes found. Going ahead with removal...
  193. )
  194.  
  195.  
  196. :::::::::::::::::::::::::
  197. :: UNINSTALLER SECTION :: -- Basically here we just brute-force every "normal" method for
  198. :::::::::::::::::::::::::    removing Java, and then resort to more painstaking methods later
  199. echo %CUR_DATE% %TIME%   Targeting individual JRE versions...>> "%LOGPATH%\%LOGFILE%"
  200. echo %CUR_DATE% %TIME%   Targeting individual JRE versions...
  201. echo %CUR_DATE% %TIME%   This might take a few minutes. Don't close this window.
  202.  
  203. :: Okay, so all JRE runtimes (series 4-7) use product GUIDs, with certain numbers that increment with each new update (e.g. Update 25)
  204. :: This makes it easy to catch ALL of them through liberal use of WMI wildcards ("_" is single character, "%" is any number of characters)
  205. :: Additionally, JRE 6 introduced 64-bit runtimes, so in addition to the two-digit Update XX revision number, we also check for the architecture
  206. :: type, which always equals '32' or '64'. The first wildcard is the architecture, the second is the revision/update number.
  207.  
  208. :: JRE 7
  209. echo %CUR_DATE% %TIME%   JRE 7...>> "%LOGPATH%\%LOGFILE%"
  210. echo %CUR_DATE% %TIME%   JRE 7...
  211. %WMIC% product where "IdentifyingNumber like '{26A24AE4-039D-4CA4-87B4-2F8__170__FF}'" call uninstall /nointeractive >> "%LOGPATH%\%LOGFILE%"
  212.  
  213. :: JRE 6
  214. echo %CUR_DATE% %TIME%   JRE 6...>> "%LOGPATH%\%LOGFILE%"
  215. echo %CUR_DATE% %TIME%   JRE 6...
  216. :: 1st line is for updates 23-xx, after 64-bit runtimes were introduced.
  217. :: 2nd line is for updates 1-22, before Oracle released 64-bit JRE 6 runtimes
  218. %WMIC% product where "IdentifyingNumber like '{26A24AE4-039D-4CA4-87B4-2F8__160__FF}'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  219. %WMIC% product where "IdentifyingNumber like '{3248F0A8-6813-11D6-A77B-00B0D0160__0}'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  220.  
  221. :: JRE 5
  222. echo %CUR_DATE% %TIME%   JRE 5...>> "%LOGPATH%\%LOGFILE%"
  223. echo %CUR_DATE% %TIME%   JRE 5...
  224. %WMIC% product where "IdentifyingNumber like '{3248F0A8-6813-11D6-A77B-00B0D0150__0}'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  225.  
  226. :: JRE 4
  227. echo %CUR_DATE% %TIME%   JRE 4...>> "%LOGPATH%\%LOGFILE%"
  228. echo %CUR_DATE% %TIME%   JRE 4...
  229. %WMIC% product where "IdentifyingNumber like '{7148F0A8-6813-11D6-A77B-00B0D0142__0}'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  230.  
  231. :: JRE 3 (AKA "Java 2 Runtime Environment Standard Edition" v1.3.1_00-25)
  232. echo %CUR_DATE% %TIME%   JRE 3 (AKA Java 2 Runtime v1.3.xx)...>> "%LOGPATH%\%LOGFILE%"
  233. echo %CUR_DATE% %TIME%   JRE 3 (AKA Java 2 Runtime v1.3.xx)...
  234. :: This version is so old we have to resort to different methods of removing it
  235. :: Loop through each sub-version
  236. FOR %%i IN (01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25) DO (
  237.     %SystemRoot%\IsUninst.exe -f"%ProgramFiles%\JavaSoft\JRE\1.3.1_%%i\Uninst.isu" -a 2>NUL
  238.     %SystemRoot%\IsUninst.exe -f"%ProgramFiles(x86)%\JavaSoft\JRE\1.3.1_%%i\Uninst.isu" -a 2>NUL
  239. )
  240. :: This one wouldn't fit in the loop above
  241. %SystemRoot%\IsUninst.exe -f"%ProgramFiles%\JavaSoft\JRE\1.3\Uninst.isu" -a 2>NUL
  242. %SystemRoot%\IsUninst.exe -f"%ProgramFiles(x86)%\JavaSoft\JRE\1.3\Uninst.isu" -a 2>NUL
  243.  
  244. :: Wildcard uninstallers
  245. echo %CUR_DATE% %TIME%   Specific targeting done. Now running WMIC wildcard catchall uninstallation...>> "%LOGPATH%\%LOGFILE%"
  246. echo %CUR_DATE% %TIME%   Specific targeting done. Now running WMIC wildcard catchall uninstallation...
  247. %WMIC% product where "name like '%%J2SE Runtime%%'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  248. %WMIC% product where "name like 'Java%%Runtime%%'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  249. %WMIC% product where "name like 'JavaFX%%'" call uninstall /nointeractive>> "%LOGPATH%\%LOGFILE%"
  250. echo %CUR_DATE% %TIME%   Done.>> "%LOGPATH%\%LOGFILE%"
  251. echo %CUR_DATE% %TIME%   Done.
  252.  
  253.  
  254. ::::::::::::::::::::::
  255. :: REGISTRY CLEANUP :: -- This is where it gets hairy. Don't read ahead if you have a weak constitution.
  256. ::::::::::::::::::::::
  257. :: If we're on XP we skip this entire block due to differences in the reg.exe binary
  258. if '%OS_VERSION%'=='XP' (
  259.     echo %CUR_DATE% %TIME% ! Registry cleanup doesn't work on Windows XP. Skipping...>> "%LOGPATH%\%LOGFILE%"
  260.    echo %CUR_DATE% %TIME% ! Registry cleanup doesn't work on Windows XP. Skipping...
  261.     goto file_cleanup
  262.     )
  263.  
  264. echo %CUR_DATE% %TIME%   Commencing registry cleanup...>> "%LOGPATH%\%LOGFILE%"
  265. echo %CUR_DATE% %TIME%   Commencing registry cleanup...
  266. echo %CUR_DATE% %TIME%   Searching for residual registry keys...>> "%LOGPATH%\%LOGFILE%"
  267. echo %CUR_DATE% %TIME%   Searching for residual registry keys...
  268.  
  269. :: Search MSIExec installer class hive for keys
  270. echo %CUR_DATE% %TIME%   Looking in HKLM\software\classes\installer\products...>> "%LOGPATH%\%LOGFILE%"
  271. echo %CUR_DATE% %TIME%   Looking in HKLM\software\classes\installer\products...
  272. reg query HKLM\software\classes\installer\products /f "J2SE Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  273. reg query HKLM\software\classes\installer\products /f "Java(TM) 6 Update" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  274. reg query HKLM\software\classes\installer\products /f "Java 7" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  275. reg query HKLM\software\classes\installer\products /f "Java*Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  276.  
  277. :: Search the Add/Remove programs list (this helps with broken Java installations)
  278. echo %CUR_DATE% %TIME%   Looking in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall...>> "%LOGPATH%\%LOGFILE%"
  279. echo %CUR_DATE% %TIME%   Looking in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall...
  280. reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f "J2SE Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  281. reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f "Java(TM) 6 Update" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  282. reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f "Java 7" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  283. reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f "Java*Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  284.  
  285. :: Search the Add/Remove programs list, x86/Wow64 node (this helps with broken Java installations)
  286. echo %CUR_DATE% %TIME%   Looking in HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall...>> "%LOGPATH%\%LOGFILE%"
  287. echo %CUR_DATE% %TIME%   Looking in HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall...
  288. reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /f "J2SE Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  289. reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /f "Java(TM) 6 Update" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  290. reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /f "Java 7" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  291. reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /f "Java*Runtime" /s | find "HKEY_LOCAL_MACHINE" >> %TEMP%\java_purge_registry_keys.txt
  292.  
  293. :: List the leftover registry keys
  294. echo %CUR_DATE% %TIME%   Found these keys...>> "%LOGPATH%\%LOGFILE%"
  295. echo %CUR_DATE% %TIME%   Found these keys...
  296. echo.>> "%LOGPATH%\%LOGFILE%"
  297. echo.
  298. type %TEMP%\java_purge_registry_keys.txt>> "%LOGPATH%\%LOGFILE%"
  299. type %TEMP%\java_purge_registry_keys.txt
  300. echo.>> "%LOGPATH%\%LOGFILE%"
  301. echo.
  302.  
  303. :: Backup the various registry keys that will get deleted (if they exist)
  304. :: We do this mainly because we're using wildcards, so we want a method to roll back if we accidentally nuke the wrong thing
  305. echo %CUR_DATE% %TIME%   Backing up keys...>> "%LOGPATH%\%LOGFILE%"
  306. echo %CUR_DATE% %TIME%   Backing up keys...
  307. if exist "%TEMP%\java_purge_registry_backup" rmdir /s /q "%TEMP%\java_purge_registry_backup" 2>NUL
  308. mkdir %TEMP%\java_purge_registry_backup >NUL
  309. :: This line walks through the file we generated and dumps each key to a file
  310. for /f "tokens=* delims= " %%a in (%TEMP%\java_purge_registry_keys.txt) do (reg query %%a) >> %TEMP%\java_purge_registry_backup\java_reg_keys_1.bak
  311.  
  312. echo.
  313. echo %CUR_DATE% %TIME%   Keys backed up to %TEMP%\java_purge_registry_backup\ >> "%LOGPATH%\%LOGFILE%"
  314. echo %CUR_DATE% %TIME%   Keys backed up to %TEMP%\java_purge_registry_backup\
  315. echo %CUR_DATE% %TIME%   This directory will be deleted at next reboot, so get it now if you need it! >> "%LOGPATH%\%LOGFILE%"
  316. echo %CUR_DATE% %TIME%   This directory will be deleted at next reboot, so get it now if you need it!
  317.  
  318. :: Purge the keys
  319. echo %CUR_DATE% %TIME%   Purging keys...>> "%LOGPATH%\%LOGFILE%"
  320. echo %CUR_DATE% %TIME%   Purging keys...
  321. echo.
  322. :: This line walks through the file we generated and deletes each key listed
  323. for /f "tokens=* delims= " %%a in (%TEMP%\java_purge_registry_keys.txt) do reg delete %%a /va /f >> "%LOGPATH%\%LOGFILE%" 2>NUL
  324.  
  325. :: These lines delete some specific Java locations
  326. :: These keys AREN'T backed up because these are specific, known Java keys, whereas above we were nuking
  327. :: keys based on wildcards, so those need backups in case we nuke something we didn't want to.
  328.  
  329. :: Delete keys for 32-bit Java installations on a 64-bit copy of Windows
  330. reg delete "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Auto Update" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  331. reg delete "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Plug-in" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  332. reg delete "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  333. reg delete "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Update" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  334. reg delete "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Web Start" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  335. reg delete "HKLM\SOFTWARE\Wow6432Node\JreMetrics" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  336.  
  337. :: Delete keys for for 32-bit and 64-bit Java installations on matching Windows architecture
  338. reg delete "HKLM\SOFTWARE\JavaSoft\Auto Update" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  339. reg delete "HKLM\SOFTWARE\JavaSoft\Java Plug-in" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  340. reg delete "HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  341. reg delete "HKLM\SOFTWARE\JavaSoft\Java Update" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  342. reg delete "HKLM\SOFTWARE\JavaSoft\Java Web Start" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  343. reg delete "HKLM\SOFTWARE\JreMetrics" /va /f>> "%LOGPATH%\%LOGFILE%" 2>NUL
  344.  
  345. echo.
  346. echo %CUR_DATE% %TIME%   Keys purged.>> "%LOGPATH%\%LOGFILE%"
  347. echo %CUR_DATE% %TIME%   Keys purged.
  348. echo %CUR_DATE% %TIME%   Registry cleanup done.>> "%LOGPATH%\%LOGFILE%"
  349. echo %CUR_DATE% %TIME%   Registry cleanup done.
  350. echo.
  351.  
  352.  
  353. ::::::::::::::::::::::::::::::::
  354. :: FILE AND DIRECTORY CLEANUP ::
  355. ::::::::::::::::::::::::::::::::
  356. :file_cleanup
  357. echo %CUR_DATE% %TIME%   Commencing file and directory cleanup...>> "%LOGPATH%\%LOGFILE%"
  358. echo %CUR_DATE% %TIME%   Commencing file and directory cleanup...
  359.  
  360. :: Kill accursed Java tasks in Task Scheduler
  361. echo %CUR_DATE% %TIME%   Removing Java tasks from the Windows Task Scheduler...>> "%LOGPATH%\%LOGFILE%"
  362. echo %CUR_DATE% %TIME%   Removing Java tasks from the Windows Task Scheduler...
  363. if exist %WINDIR%\tasks\Java*.job del /F /Q %WINDIR%\tasks\Java*.job >> "%LOGPATH%\%LOGFILE%"
  364. if exist %WINDIR%\System32\tasks\Java*.job del /F /Q %WINDIR%\System32\tasks\Java*.job >> "%LOGPATH%\%LOGFILE%"
  365. if exist %WINDIR%\SysWOW64\tasks\Java*.job del /F /Q %WINDIR%\SysWOW64\tasks\Java*.job >> "%LOGPATH%\%LOGFILE%"
  366. echo.
  367.  
  368. :: Kill the accursed Java Quickstarter service
  369. sc query JavaQuickStarterService >NUL
  370. if not %ERRORLEVEL%==1060 (
  371.     echo %CUR_DATE% %TIME%   De-registering and removing Java Quickstarter service...>> "%LOGPATH%\%LOGFILE%"
  372.     echo %CUR_DATE% %TIME%   De-registering and removing Java Quickstarter service...
  373.     net stop JavaQuickStarterService >> "%LOGPATH%\%LOGFILE%" 2>NUL
  374.     sc delete JavaQuickStarterService >> "%LOGPATH%\%LOGFILE%" 2>NUL
  375. )
  376.  
  377. :: Kill the accursed Java Update Scheduler service
  378. sc query jusched >NUL
  379. if not %ERRORLEVEL%==1060 (
  380.     echo %CUR_DATE% %TIME%   De-registering and removing Java Update Scheduler service...>> "%LOGPATH%\%LOGFILE%"
  381.     echo %CUR_DATE% %TIME%   De-registering and removing Java Update Scheduler service...
  382.     net stop jusched >> "%LOGPATH%\%LOGFILE%" 2>NUL
  383.     sc delete jusched >> "%LOGPATH%\%LOGFILE%" 2>NUL
  384. )
  385.  
  386. :: This is the Oracle method of disabling the Java services. 99% of the time these commands aren't required.
  387. if exist "%ProgramFiles(x86)%\Java\jre6\bin\jqs.exe" "%ProgramFiles(x86)%\Java\jre6\bin\jqs.exe" -disable>> "%LOGPATH%\%LOGFILE%"
  388. if exist "%ProgramFiles(x86)%\Java\jre7\bin\jqs.exe" "%ProgramFiles(x86)%\Java\jre7\bin\jqs.exe" -disable>> "%LOGPATH%\%LOGFILE%"
  389. if exist "%ProgramFiles%\Java\jre6\bin\jqs.exe" "%ProgramFiles%\Java\jre6\bin\jqs.exe" -disable>> "%LOGPATH%\%LOGFILE%"
  390. if exist "%ProgramFiles%\Java\jre7\bin\jqs.exe" "%ProgramFiles%\Java\jre7\bin\jqs.exe" -disable>> "%LOGPATH%\%LOGFILE%"
  391. if exist "%ProgramFiles(x86)%\Java\jre6\bin\jqs.exe" "%ProgramFiles(x86)%\Java\jre6\bin\jqs.exe" -unregister>> "%LOGPATH%\%LOGFILE%"
  392. if exist "%ProgramFiles(x86)%\Java\jre7\bin\jqs.exe" "%ProgramFiles(x86)%\Java\jre7\bin\jqs.exe" -unregister>> "%LOGPATH%\%LOGFILE%"
  393. if exist "%ProgramFiles%\Java\jre6\bin\jqs.exe" "%ProgramFiles%\Java\jre6\bin\jqs.exe" -unregister>> "%LOGPATH%\%LOGFILE%"
  394. if exist "%ProgramFiles%\Java\jre7\bin\jqs.exe" "%ProgramFiles%\Java\jre7\bin\jqs.exe" -unregister>> "%LOGPATH%\%LOGFILE%"
  395. msiexec.exe /x {4A03706F-666A-4037-7777-5F2748764D10} /qn /norestart
  396.  
  397. :: Nuke 32-bit Java installation directories
  398. if exist "%ProgramFiles(x86)%" (
  399.     echo %CUR_DATE% %TIME%   Removing "%ProgramFiles(x86)%\Java\jre*" directories...>> "%LOGPATH%\%LOGFILE%"
  400.     echo %CUR_DATE% %TIME%   Removing "%ProgramFiles(x86)%\Java\jre*" directories...
  401.     for /D /R "%ProgramFiles(x86)%\Java\" %%x in (j2re*) do if exist "%%x" rmdir /S /Q "%%x">> "%LOGPATH%\%LOGFILE%"
  402.     for /D /R "%ProgramFiles(x86)%\Java\" %%x in (jre*) do if exist "%%x" rmdir /S /Q "%%x">> "%LOGPATH%\%LOGFILE%"
  403.     if exist "%ProgramFiles(x86)%\JavaSoft\JRE" rmdir /S /Q "%ProgramFiles(x86)%\JavaSoft\JRE" >> "%LOGPATH%\%LOGFILE%"
  404. )
  405.  
  406. :: Nuke 64-bit Java installation directories
  407. echo %CUR_DATE% %TIME%   Removing "%ProgramFiles%\Java\jre*" directories...>> "%LOGPATH%\%LOGFILE%"
  408. echo %CUR_DATE% %TIME%   Removing "%ProgramFiles%\Java\jre*" directories...
  409. for /D /R "%ProgramFiles%\Java\" %%x in (j2re*) do if exist "%%x" rmdir /S /Q "%%x">> "%LOGPATH%\%LOGFILE%"
  410. for /D /R "%ProgramFiles%\Java\" %%x in (jre*) do if exist "%%x" rmdir /S /Q "%%x">> "%LOGPATH%\%LOGFILE%"
  411. if exist "%ProgramFiles%\JavaSoft\JRE" rmdir /S /Q "%ProgramFiles%\JavaSoft\JRE" >> "%LOGPATH%\%LOGFILE%"
  412.  
  413. :: Nuke Java installer cache ( thanks to cannibalkitteh )
  414. echo %CUR_DATE% %TIME%   Purging Java installer cache...>> "%LOGPATH%\%LOGFILE%"
  415. echo %CUR_DATE% %TIME%   Purging Java installer cache...
  416. :: XP VERSION
  417. if %OS_VERSION%==XP (
  418.     :: Get list of users, put it in a file, then use it to iterate through each users profile, deleting the AU folder
  419.     dir "%SystemDrive%\Documents and Settings\" /B > %TEMP%\userlist.txt
  420.     for /f "tokens=* delims= " %%a in (%TEMP%\userlist.txt) do (
  421.         if exist "%SystemDrive%\Documents and Settings\%%a\AppData\LocalLow\Sun\Java\AU" rmdir /S /Q "%SystemDrive%\Documents and Settings\%%a\AppData\LocalLow\Sun\Java\AU" 2>NUL
  422.     )
  423.     for /D /R "%SystemDrive%\Documents and Settings\" %%x in (jre*) do if exist "%%x" rmdir /S /Q "%%x" 2>NUL
  424. ) else (
  425.     :: ALL OTHER VERSIONS OF WINDOWS
  426.     :: Get list of users, put it in a file, then use it to iterate through each users profile, deleting the AU folder
  427.     dir %SystemDrive%\Users /B > %TEMP%\userlist.txt
  428.     for /f "tokens=* delims= " %%a in (%TEMP%\userlist.txt) do rmdir /S /Q "%SystemDrive%\Users\%%a\AppData\LocalLow\Sun\Java\AU" 2>NUL
  429.     :: Get the other JRE directories
  430.     for /D /R "%SystemDrive%\Users" %%x in (jre*) do rmdir /S /Q "%%x" 2>NUL
  431.     )
  432.  
  433. :: Miscellaneous stuff, sometimes left over by the installers
  434. echo %CUR_DATE% %TIME%   Searching for and purging other Java Runtime-related directories...>> "%LOGPATH%\%LOGFILE%"
  435. echo %CUR_DATE% %TIME%   Searching for and purging other Java Runtime-related directories...
  436. del /F /Q %SystemDrive%\1033.mst >> "%LOGPATH%\%LOGFILE%" 2>NUL
  437. del /F /S /Q "%SystemDrive%\J2SE Runtime Environment*" >> "%LOGPATH%\%LOGFILE%" 2>NUL
  438. echo.
  439.  
  440. echo %CUR_DATE% %TIME%   File and directory cleanup done.>> "%LOGPATH%\%LOGFILE%"
  441. echo %CUR_DATE% %TIME%   File and directory cleanup done.
  442. echo. >> "%LOGPATH%\%LOGFILE%"
  443. echo.
  444.  
  445.  
  446. :::::::::::::::::::::::::
  447. :: JAVA REINSTALLATION :: -- If we wanted to reinstall the JRE after cleanup, this is where it happens
  448. :::::::::::::::::::::::::
  449. :: x64
  450. if %REINSTALL_JAVA_x64%==yes (
  451.     echo %CUR_DATE% %TIME% ! Variable "REINSTALL_JAVA_x64" was set to 'yes'. Now installing %JAVA_BINARY_x64%...>> "%LOGPATH%\%LOGFILE%"
  452.     echo %CUR_DATE% %TIME% ! Variable "REINSTALL_JAVA_x64" was set to 'yes'. Now installing %JAVA_BINARY_x64%...
  453.     "%JAVA_LOCATION_x64%\%JAVA_BINARY_x64%" %JAVA_ARGUMENTS_x64%
  454.     java -version
  455.     echo Done.>> "%LOGPATH%\%LOGFILE%"
  456.     )
  457.  
  458. :: x86
  459. if %REINSTALL_JAVA_x86%==yes (
  460.     echo %CUR_DATE% %TIME% ! Variable "REINSTALL_JAVA_x86" was set to 'yes'. Now installing %JAVA_BINARY_x86%...>> "%LOGPATH%\%LOGFILE%"
  461.     echo %CUR_DATE% %TIME% ! Variable "REINSTALL_JAVA_x86" was set to 'yes'. Now installing %JAVA_BINARY_x86%...
  462.     "%JAVA_LOCATION_x86%\%JAVA_BINARY_x86%" %JAVA_ARGUMENTS_x86%
  463.     java -version
  464.     echo Done.>> "%LOGPATH%\%LOGFILE%"
  465.     )
  466.  
  467. :: Done.
  468. echo %CUR_DATE% %TIME%   Registry hive backups: %TEMP%\java_purge_registry_backup\>> "%LOGPATH%\%LOGFILE%"
  469. echo %CUR_DATE% %TIME%   Registry hive backups: %TEMP%\java_purge_registry_backup\
  470. echo %CUR_DATE% %TIME%   Log file: "%LOGPATH%\%LOGFILE%">> "%LOGPATH%\%LOGFILE%"
  471. echo %CUR_DATE% %TIME%   Log file: "%LOGPATH%\%LOGFILE%"
  472. echo %CUR_DATE% %TIME%   JAVA NUKER COMPLETE. Recommend rebooting and washing your hands.>> "%LOGPATH%\%LOGFILE%"
  473. echo %CUR_DATE% %TIME%   JAVA NUKER COMPLETE. Recommend rebooting and washing your hands.
  474.  
  475. :: Return exit code to SCCM/PDQ Deploy/PSexec/etc
  476. exit /B %EXIT_CODE%
Advertisement
Add Comment
Please, Sign In to add comment