Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

script to deploy java update (msi) to a specific folder

Avatar of richardstuartpowell
richardstuartpowellFlag for United Kingdom of Great Britain and Northern Ireland asked on
JavaVB Script
3 Comments1 Solution836 ViewsLast Modified:
Dear experts

I have a script (shown below) that checks the version of java installed on a PC and if the version installed is older / different to the version we want to deploy, then removes it and installs a newer version (Update 17).  The script works perfectly - apart from the fact that we now need to make sure that Java gets installed to a specific folder (and not the default location) - can someone show me what needs to be included in my script to get the MSI installer to install Java to a specific folder and where in my script this would need to go please?

'# Galen Dobbs - 13:20 23/03/2009
'# Uninstalls all but the chosen version of Java Runtime.
'# If the current version is not installed, it installs it from the specified path.
'# Based on a script by 'Daz' from Appdeploy.com message boards.
'# http://www.appdeploy.com/messageboards/tm.asp?m=29809


Option Explicit

Dim wshShell, fso, strLogFile, ts, strTempDir, strTempISS, strUnString, tsIn
Dim strUninstLine, CLSID, search5, search6, search7, strJRE1, strDisplayName, strDisplayVersion
Dim strPublisher, strUninstallString, strJREUninstallString, strJREDisplayName
Dim search1, search2, search3, search4, strJREUninstallStringNEW, ret, strUninstCMD
Dim tsISS, strSetupexe, qVal, strComputername, strCurrentVersion, strInstallMST
Dim searchCurVer, CurVerFound, strArrayCount, strLogPath, strInstallCMD, strInstallMSI, strInstallLog

Dim arrayJREDisplayName()
Dim arrayJREUninstallString()

'# Change this to match the version that you don't want to have it uninstall
strCurrentVersion = "Java(TM) 6 Update 18"

'# Set these to the desired log path and current version installer location
strLogPath = "\\Server\share\JAVALOG\"
strInstallMSI = "\\server\share\jre1.6.0_18\jre1.6.0_18.msi"
strInstallMST = "\\server\share\jre1.6.0_18\sp1033.mst"

qVal = 0
strArrayCount = 0
ReDim arrayJREDisplayName(strArrayCount)
ReDim arrayJREUninstallString(strArrayCount)

Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

strComputername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

'# Set this to the appropriate command line settings to do a silent MSI install
strInstallLog = strLogPath & "Java_Install_" & strComputername & ".log"
strInstallCMD = "msiexec /I """ & strInstallMSI & """ /t """ & strInstallMST & """ /QN /Lime """ & strInstallLog & """"

If Not fso.FolderExists(strLogPath) Then fso.CreateFolder(strLogPath)
strLogFile = strLogPath & "Java_Uninstall_" & strComputername & ".log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLine String(120, "_")
ts.WriteLine String(120, "¯")
ts.WriteLine Now() & " - Java Runtime(s) uninstallation started..."
ts.WriteLine String(120, "_") & vbCrlf

'# Generate Registry extracts from 'Uninstall' keys.
PreFlight()

'# Kill Java Processes
KillProc()

strTempDir = wshShell.ExpandEnvironmentStrings("%temp%")
strTempISS = strTempDir & "\iss"
strUnString = " -s -a /s /f1"
Set tsIn = fso.OpenTextFile(strTempDir & "\uninstall.tmp", 1)

If Not fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS)

Do While Not tsIn.AtEndOfStream
   strUninstLine = tsIn.ReadLine
   CLSID = Mid(strUninstLine, 73, 38)
   search5 = Instr(strUninstLine, "JRE 1")
   search6 = Instr(strUninstLine, "]")
   If search5 > 0 AND search6 > 0 Then
       strJRE1 = Replace(Mid(strUninstLine, search5, search6),"]","")  
   End If

   On Error Resume Next

   strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName")
   strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion")
   strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher")
   strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString")

   strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString")
   strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName")

   On Error Goto 0

   'Search for presence of Java and Sun in DisplayName and Publisher
   search1 = Instr(1, strDisplayName, "Java", 1)
   search2 = Instr(1, strPublisher, "Sun", 1)
   search3 = Instr(1, strDisplayName, "J2SE", 1)
   search4 = Instr(1, strUninstallString, "setup.exe", 1)
   search7 = InStr(1, strDisplayName, "Development", 1) + InStr(1, strDisplayName, "Java DB", 1)

   'See if it is the current version
   searchCurVer = InStr(1, strDisplayName, strCurrentVersion, 1)

   'If it is, Show that the current version is found
   If searchCurVer > 0 Then
   CurVerFound = True

   ElseIf strJREUninstallString <> "" Then
       '# JRE 1 found
       strJREUninstallStringNEW = Replace(strJREUninstallString," -f"," -s -a /s /f")
   redim Preserve arrayJREDisplayName(strArrayCount)
   redim Preserve arrayJREUninstallString(strArrayCount)
       arrayJREDisplayName(strArrayCount) = " - Found Old JRE: " & strDisplayName & "  - Version: " & strDisplayVersion & ", Uninstalling..."
   arrayJREUninstallString(strArrayCount) = strJREUninstallStringNEW
   strArrayCount = strArrayCount + 1

   ElseIf search7 = 0 And search1 > 0 Or search3 > 0 And search2 > 0 Then
       strUninstCMD = "msiexec.exe /x " & CLSID & " /norestart /qn"

       If search4 > 0 Then
           '# Old InstallShield setup found
           Set tsISS = fso.OpenTextFile(strTempISS & "\" & CLSID & ".iss", 2, True)
 
           'Create Response file for any Java Version
           tsISS.WriteLine "[InstallShield Silent]"
           tsISS.WriteLine "Version=v6.00.000"
           tsISS.WriteLine "File=Response File"
           tsISS.WriteLine "[File Transfer]"
           tsISS.WriteLine "OverwrittenReadOnly=NoToAll"
           tsISS.WriteLine "[" & CLSID & "-DlgOrder]"
           tsISS.WriteLine "Dlg0=" & CLSID & "-SprintfBox-0"
           tsISS.WriteLine "Count=2"
           tsISS.WriteLine "Dlg1=" & CLSID & "-File Transfer"
           tsISS.WriteLine "[" & CLSID & "-SprintfBox-0]"
           tsISS.WriteLine "Result=1"
           tsISS.WriteLine "[Application]"
           tsISS.WriteLine "Name=Java 2 Runtime Environment, SE v1.4.0_01"
           tsISS.WriteLine "Version=1.4.0_01"
           tsISS.WriteLine "Company=JavaSoft"
           tsISS.WriteLine "Lang=0009"
           tsISS.WriteLine "[" & CLSID & "-File Transfer]"
           tsISS.WriteLine "SharedFile=YesToAll"
           tsISS.Close

           strSetupexe = Left(strUninstallString, search4 + 9)
           strUninstCMD =  strSetupexe & strUnString & Chr(34) & strTempISS & "\" & CLSID & ".iss" & Chr(34)
       End If

   redim Preserve arrayJREDisplayName(strArrayCount)
   redim Preserve arrayJREUninstallString(strArrayCount)
   arrayJREDisplayName(strArrayCount) = " - Found Old JRE: " & strDisplayName & "    - Version: " & strDisplayVersion & ", Uninstalling..."
   arrayJREUninstallString(strArrayCount) = strUninstCMD
   strArrayCount = strArrayCount + 1
       
   End If

Loop

tsIn.Close

Dim I
If CurVerFound AND strArrayCount > 0 Then
   ts.Writeline Now() & " - Current Version: " & strCurrentVersion & " found, continuing with uninstalls..."
   For I = LBOUND(arrayJREDisplayName) to UBOUND(arrayJREDisplayName)
       ts.WriteLine Now() & arrayJREDisplayName(I)
       ts.WriteLine Now() & " - Uninstall String sent: " & arrayJREUninstallString(I)
       ret = wshShell.Run(arrayJREUninstallString(I) , 0, True)
       ts.WriteLine Now() & " - Return: " & ret
       If ret <> 0 And ret <> 3010 Then qVal = 1
   Next

ElseIf CurVerFound AND strArrayCount = 0 Then
   ts.WriteLine Now() & " - Current version, " & strCurrentVersion & ", found."  
   ts.WriteLine Now() & " - No Old Java Runtime versions are installed."
   qVal = 99

ElseIf Not CurVerFound Then
   
   ts.WriteLine Now() & " - Current Java version, " & strCurrentVersion & ", not found, installing it."
   ts.WriteLine Now() & " - Running Command: " & strInstallCMD
   ret = wshShell.Run(strInstallCMD , 0, True)
   If ret <> 0 AND ret<> 3010 Then
       ts.WriteLine Now() & " - Failed to Install Java, see " & strInstallLog & " for more details.  Exiting Script."
       qVal = 1
   ElseIf strArrayCount > 0 Then
       ts.WriteLine Now() & " - Successfully installed " & strCurrentVersion & ", and logged to " & strInstallLog & "."
       For I = LBOUND(arrayJREDisplayName) to UBOUND(arrayJREDisplayName)
       ts.WriteLine Now() & arrayJREDisplayName(I)
       ts.WriteLine Now() & " - Uninstall String sent: " & arrayJREUninstallString(I)
       ret = wshShell.Run(arrayJREUninstallString(I) , 0, True)
       ts.WriteLine Now() & " - Return: " & ret
       If ret <> 0 And ret <> 3010 Then qVal = 1
   Next
   ElseIf strArrayCount = 0 Then
      ts.WriteLine Now() & " - Successfully installed " & strCurrentVersion & ", and logged to " & strInstallLog & "."
   ts.WriteLine Now() & " - No Old Java Runtime versions are installed."
       qVal = 99
   End If
End If

ts.WriteLine String(120, "_")
ts.WriteLine String(120, "¯")
ts.Close
fso.DeleteFolder(strTempISS)
fso.DeleteFile(strTempDir & "\uninstall.tmp")

WScript.Quit(qVal)

Sub PreFlight()
   '# Creates temp files containing extracts from registry 'Uninstall' keys.
   Dim wshShell, fso, sTemp
   Set wshShell = CreateObject("WScript.Shell")
   Set fso = CreateObject("Scripting.FileSystemObject")
   sTemp = wshShell.ExpandEnvironmentStrings("%temp%")
   wshShell.Run "REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall", 0, True
   wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""{"" | find /i ""}]"" > %temp%\uninstall.tmp ", 0, True
   wshShell.Run "cmd /c type %temp%\registry.tmp | find /i ""JRE 1"" >> %temp%\uninstall.tmp ", 0, True
   If Not fso.FileExists(sTemp & "\uninstall.tmp") Then
       ts.WriteLine Now() & " - No input - %temp%\uninstall.tmp Reg extract not created."
       ts.WriteLine String(120, "_")
       ts.WriteLine String(120, "¯")
       ts.Close
       WScript.Quit(1)
   End If
End Sub

Sub KillProc()
   '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
   Dim wshShell
   Set wshShell = CreateObject("WScript.Shell")
   wshShell.Run "Taskkill /F /IM jusched.exe /T", 0, True
   wshShell.Run "Taskkill /F /IM jqs.exe /T", 0, True
End Sub
ASKER CERTIFIED SOLUTION
Avatar of jostrander
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers