'Alan Kaplan, alan dot kaplan at va dot gov '1-17-04 Updated for Win7 and later when InstallDate2 is null, get inst 'date from creation date of package. '12-30-11 some code cleanup '11-17-11 search for name instead of product ID makes for much faster script. '8-29-2008 Initial version Option Explicit Const adVarChar = 200 Const MaxCharacters = 255 Const adFldIsNullable = 32 Const adInteger = 3 Const adDate = 7 Dim oWMI, oRS Dim strComputer Dim wshShell:Set wshShell = WScript.CreateObject("WScript.Shell") dim fso: set fso = CreateObject("Scripting.FileSystemObject") REM set test to False to Uninstall Dim Test:Test = False If (Not IsCScript()) Then 'If not CScript, re-run with cscript... dim quote, strArgs, i quote=chr(34) For i = WScript.Arguments.Count -1 to 0 Step -1 strArgs = WScript.Arguments(i) & Space(1) & strArgs Next WshShell.Run "CScript.exe " & quote & WScript.ScriptFullName & quote & space(1) & strArgs, 1, true WScript.Quit '...and stop running as WScript End If If WScript.Arguments.Count = 1 Then strComputer = WScript.Arguments(0) Else strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") strComputer = InputBox("Uninstall old Java from what PC","PC Name?",strComputer) End If If strcomputer = "" Then WScript.Quit strComputer = UCase(strComputer) On Error Resume Next Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") If Err.Number <> 0 Then WScript.Echo Err.Description WScript.Quit End If 'ref http://www.devguru.com/Technologies/ado/quickref/record_fieldscollection.html 'create disconnected recordset -- note the ADOR Set oRS = CreateObject("ADOR.Recordset") oRS.Fields.Append "Caption", adVarChar, MaxCharacters oRS.Fields.Append "Version", adVarChar, MaxCharacters oRS.Fields.Append "ID", adVarChar, MaxCharacters oRS.Fields.Append "Date", adDate, adFldIsNullable oRS.Open WScript.Echo "Collecting a list of Java installations on " & strComputer & ". This can take a while, please wait ..." Dim dStart:dStart=Now WScript.Echo "Start time: " & dStart GetList () WScript.Echo "Elapsed time in seconds to collect list: " & DateDiff("s", dStart, now) oRS.Sort = "Date ASC" oRS.MoveFirst If oRS.BOF And oRS.EOF Then MsgBox "Java not installed.",vbInformation + vbOKOnly,strComputer WScript.Quit End If If oRS.RecordCount = 1 Then MsgBox "Only one version of Java is installed, " & oRS.Fields.Item("Caption")& Space(1) & oRS.Fields.Item("Version"), _ vbInformation + vbOKOnly,strComputer WScript.Quit Else Wscript.Echo "Skipping newest, " & oRS.Fields.Item("Caption")& Space(1) & oRS.Fields.Item("Version") oRS.MoveNext 'Skip newest Do Until oRS.EOF Uninstall oRS.Fields.Item("ID"), oRS.Fields.Item("Caption") oRS.MoveNext Loop End If oRS.Close WScript.Echo "Total elapsed time in seconds: " & DateDiff("s", dStart, now) Function GetList() Dim strQuery, colItems, oItem, strDate, strCaption strQuery = "SELECT caption, version, identifyingnumber, localPackage, installdate2 FROM Win32_Product where caption like '%Java%'" Set colItems = oWMI.ExecQuery(strQuery,,48) For Each oItem In colItems strDate = SWInstallDate (oItem.Installdate2, oItem.LocalPackage ) strCaption = oItem.Caption If ((InStr(1,strCaption,"script",1) = 0 ) And (InStr(1,strCaption,"auto updater",1) = 0)) Then oRS.AddNew oRS("Caption") = strCaption oRS("Version") = (oItem.Version) oRS("ID") = (oItem.IdentifyingNumber) oRS("Date") = strDate oRS.Update End If Next End Function Function SWInstallDate (WMIInstDate, localPackage) Dim f,strUNCPath If isNull(WMIInstDate) Then 'convert install package path to a UNC strUNCPath = "\\"&strComputer&"\"& Replace(localPackage,":","$") Set f= fso.GetFile(strUNCPath) SWInstallDate = f.DateCreated Else SWInstallDate = WMIDateStringToDate(oItem.InstallDate2) End If End Function Sub Uninstall(strID, strName) Dim colSoftware, objSoftware, retval Set colSoftware = oWMI.ExecQuery("Select name from Win32_Product where identifyingNumber ='" & strID & "'") If isnull(colsoftware.count) or Err.Number <> 0 Then MsgBox strName & " was not found on " & strcomputer,vbcritical+vbokonly,"Fatal Error" WScript.Quit End If For Each objSoftware in colSoftware WScript.Echo "Uninstalling " & objSoftware.Name & " from " & strComputer If Not (Test) Then retval = objSoftware.Uninstall() If retval = 0 Then WScript.Echo "Successfully uninstalled " & ObjSoftware.Name & " from " & strComputer Else Wscript.Echo "Failed with error code " & retval End If Else WScript.Echo "Test mode. Would have uninstalled " & ObjSoftware.Name & " from " & strComputer End If Next End Sub Function WMIDateStringToDate(dtmDate) WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _ Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _ & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2)) End Function Function IsCScript() If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = True Else IsCScript = False End If End Function