Uninstall Program by Name Only, Kill Dependent Processes & Force MI Check-In via Script (w-Java Example)

_______________________________________________________

Notes:

1) This article goes along nicely with my earlier post on finding previous versions of applications:

http://www.itninja.com/blog/view/using-regex-in-smart-labels-to-find-lower-versioned-software-w-java-example

2) The script in this post uses vbscript - if you prefer powershell, please see dchristian's excellent post here:

http://www.itninja.com/blog/view/how-to-uninstall-program-by-name-only

3) The original Java uninstall script comes from this post:

http://www.itninja.com/question/silent-uninstall-java-all-versions

_______________________________________________________

Background:

Although this setup covers Java, it should be fairly easily tweaked for other apps, as the first parts of the script target processes and programs by name.

*This script works regardless of whether users are logged in or not.*

I played with a number of different solutions to pull all instances of Java from machines with (often multiple copies of) the old versions - that is to say, versions that the current releases don't automatically uninstall during installation.  I looked into dchristian's powershell script, but stopped when I ran into an issue with signing scripts (something I'll play with later, I'm sure).  Other vbscript+batch file solutions weren't running consistently when deployed from the KBOX, so I made a pure a vbscript routine by combining an uninstall script that worked *very* consistently to uninstall Java with some other functions I found useful. 

I'll give my usual disclaimer - I'm not a programmer, so there's probably places this script could be improved, but it gets the job done and doesn't crash the PC.  That being said, if you see any ways to make it run faster/safer/etc, please let me know.

_______________________________________________________

Process:

*Smart Label*

- helps determine which machines to target

*Script*

- kills all Java dependent/related processes

- uninstalls all instances of Java and related apps by name

     * ones in my environment anyways, YMMV

- runs a client initiated MI check-in

*Managed Install*

- installs Java 7u4

_______________________________________________________

Tested on WinXP x86 SP3 (English & French) with:

Java 2 Runtime Environment, SE v1.4.2_09

Java 2 Runtime Environment, SE v1.4.2_10

Java 2 Runtime Environment, SE v1.4.2_11

J2SE Runtime Environment 5.0 Update 5

J2SE Runtime Environment 5.0 Update 6

J2SE Runtime Environment 5.0 Update 8

Java(TM) 6 Update 1

Java(TM) 6 Update 2

Java(TM) 6 Update 3

Java(TM) 6 Update 4

Java(TM) 6 Update 5

Java(TM) 6 Update 6

Java(TM) 6 Update 7

Java(TM) 6 Update 18

Java(TM) 6 Update 31

 

Tested on Win7 x64 SP1 with:

Java(TM) 6 Update 31

_______________________________________________________

*Script Type*

Online KScript

*Name*

Uninstall Old Java Versions & Run Java 7u4 MI

*Description*

Kills all Java dependent processes, uninstalls all versions of Java, initiates Inventory run so Java 7u4 managed install will start.

*Status*

Production

*Enabled*

yes

*Pick Specific OS Versions*

all

*Run As*

Run As Local System

*Scheduling*

Don't Run on a Schedule

*Dependencies*

killjava.vbs

*Task 1*

**Verify**

Always Fail

**Remediation**

Launch a program...

*Directory:  $(KBOX_SYS_DIR)

*File:  cscript.exe

*Wait for startup: yes

*Parameters: "$(KACE_DEPENDENCY_DIR)\killjava.vbs"

**On Remediation Success**

Message:  successfully ran killjava script

**On Remediation Failure**

Message:  failed to run killjava script

_______________________________________________________

'Kill Java Script

'

'Kills all java-related processes

'Uninstalls all instances of Java

'#Uninstall Script Written By Dylan Ogle & Nainesh Bhavan - November 2011#

'Checks to see if OS is x86 or x64

'Runs MI check-in task to install Java 7u4

On Error Resume Next

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Kill iexplorer.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'iexplorer.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill iexplore.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill firefox.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'firefox.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill chrome.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'chrome.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill java.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'java.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill javaw.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'javaw.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill jusched.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'jusched.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Kill jqs.exe process (unless running under SYSTEM account)

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'jqs.exe'")

For Each objProcess in colProcessList

objProcess.Terminate()

Next

'Uninstall Java 2 Runtime Environment, J2SE Runtime Environment

Set colJava4dot3 = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'Java 2 Runtime Environment Standard Edition %'")

For Each objSoftware in colJava4dot3

objSoftware.Uninstall()

Next

'Uninstall Java 2 Runtime Environment, J2SE Runtime Environment

Set colJava4dot3 = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'J2SE Runtime Environment %'")

For Each objSoftware in colJava4dot3

objSoftware.Uninstall()

Next

'Uninstall Java 2 Runtime Environment, SE *

Set colJava4dot3 = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'Java 2 Runtime Environment, SE %'")

For Each objSoftware in colJava4dot3

objSoftware.Uninstall()

Next

'Uninstall Java(TM) 6 Update *

Set colJava6dot = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'Java(TM) 6 Update %'")

For Each objSoftware in colJava6dot

objSoftware.Uninstall()

Next

'Uninstall Java(TM) 7 Update *

Set colJava6dot = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'Java(TM) 7 Update %'")

For Each objSoftware in colJava6dot

objSoftware.Uninstall()

Next

'Uninstall Java(TM) 7 *

Set colJava7 = objWMIService.ExecQuery("Select * from Win32_Product Where Name like 'Java(TM) %'")

For Each objSoftware in colJava7

objSoftware.Uninstall()

Next

'find OS bitness and run MI check-in from appropriate folder

Dim WshShell,runStr

set WshShell = WScript.CreateObject("WScript.Shell")

OsType = wshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")

If OsType = "x86" then

runStr = Chr(34) & "C:\Program Files\Dell\KACE\runkbot.exe" & Chr(34) & " -s 6 0"

WshShell.run runStr

elseif OsType = "AMD64" then

runStr = Chr(34) & "C:\Program Files (x86)\Dell\KACE\runkbot.exe" & Chr(34) & " -s 6 0"

WshShell.run runStr

end if

_______________________________________________________

Java 7u4 Managed Install

*Software*

Java(TM) 7 Update 4 (7.0.40)

*Installation Command*

Configure Manually

msiexec.exe /i jre1.7.0_04.msi /qn

*Managed Action*

Execute anytime (next available)

*Limit Deployment to Machines*

instjava7u4 Label (populated by selecting online machines from javapre6u31 Smart Label)

_______________________________________________________

Conclusion:

Although I'm using the Smart Label discussed in the referenced post to identify machines that are in need of remediation (i.e. need random old versions of Java removed) and to push the Script (at a scheduled time at night or manually during the day for affected remote machines), I am not using the Smart Label for the Managed Install - one reason being that I need to time the push of the script to avoid killing potentially business-critical browser sessions (which is a necessity to remove old & install new Java without failing/rebooting). The second is that if they are added to the MI before the script runs, that's one more version of Java to pull (unless I redid the script to skip version 7 installs altogether, of course).  Lastly, I prefer having the control of adding clients to the MI once I determine the old version(s) of Java have been pulled. 

Of course (you are probably saying), once the script has run and the machines have been inventoried, they will drop out of that Smart Label.  That is why I have a second manual Label that I've added all Smart Label machines to, so I have a point of reference (i.e. those in the manual Label, but not in the Smart Label, need added to the MI).

If that sounds overly complicated and you wonder why I don't just add the script to the MI and be done with it, this script is fairly aggressive and CPU intensive, so I only want to target the relatively small group of machines with this "condition".  For the other machines (say running Java 6u31), I'll most likely create a second MI with a batch file that targets 6u31 via a very specific "msiexec /x" command, which is much faster and less resource intensive - my machines are rather old and I try not to nail them too much, particularly if there's a user on the other end.

Hope something in there helps!

John