VBScript to Uninstall any version of Java previously installed and then install the new version
Hi Guys
I created the below script which works fine for the most part. It uninstalls any previous versions of Java and installs the new one as expected. The problem arises when I try to install the second package. I have two versions of this script. One for 32-bit and one for 64-bit. If I install one, it installs just fine but if I install the second package after the first one, in case someone wants both of them, then I get the below error.
Install.vbs(55, 6) WshShell.Run: Unable to wait for process.
I am running into this issue trying to avoid the uninstallation of the other package. If I install 32-bit first and then install 64-bit, I don't want the second package to uninstall the first one. Which is why on line 49, I am trying to make the script exit the subroutine but it's not exiting. and continues on to the next section of the subroutine and errors out.
If only one version was to be package, I would have been done. But I have to package both the 32 and 64 bit packages and I can't get the script to work the way I want.
The other option is to create a simple install and uninstall for both the packages and create a java cleaner to remove previous packages separately but i'd really like this to work.
Below is the script. Any help would be appreciated.
'Uninstalling Previous Versions
Const HKLM = &h80000002
Const SearchValue = "DisplayName"
Const MatchData = "JAVA"
Set objShell = WScript.CreateObject("WScript.Shell")
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
strProgramFiles = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%" & "\")
If InStr(strProgramFiles,"%") Then
StartKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
FindAppKey HKLM, StartKey, SearchValue, MatchData
Else
StartKey1 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
StartKey2 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
FindAppKey HKLM, StartKey1, SearchValue, MatchData
FindAppKey HKLM, StartKey2, SearchValue, MatchData
End If
'Install Java 8u221
Dim oShell, sDir, strCmd, sParam, sLogFile, sCom
sDir = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
strCmd = "\jre-8u221-windows-x64.exe"
sParam= "/s /l"
sLogFile= "C:\Support\Java_v8u221_x64_Install.log"
sCom=(chr(34) & sDir & strCmd & chr(34)) & " " & sParam & " " & (chr(34) & sLogFile & chr(34))
wscript.echo "Running command " & sCom
Set oShell = CreateObject ("WScript.Shell")
oShell.Run sCom, 6, True
'Waiting for process to finish
Call WaitForProcess
'Subroutines
Sub FindAppKey(root, key, value, data)
Dim CurrentVerx86, CurrentVerx64, CurrentVer
reg.EnumValues HKLM, key, names, types
If Not IsNull(names) Then
For Each name In names
If name = value Then
reg.GetStringValue HKLM, key, name, regdata
strRegData = UCase(regdata)
CurrentVerx86 = "JAVA 8 UPDATE 221"
CurrentVerx64 = "JAVA 8 UPDATE 221 (64-BIT)"
If InStr(strRegData, data) Then
If StrComp(strRegData,CurrentVerx86) = 0 OR StrComp(strRegData,CurrentVerx64) = 0 Then
Exit Sub
Else
wscript.echo "Exit Sub didn't work."
reg.GetStringValue HKLM, key, "UninstallString", strPdtCode
strUninstallString = strPdtCode & " /qn REBOOT=ReallySuppress"
objShell.Run strUninstallString,0,True
End If
End If
End If
Next
End If
'value not found in current key => continue search in subkeys
reg.EnumKey root, key, subkeys
If Not IsNull(subkeys) Then
For Each sk In subkeys
FindAppKey root, key & "\" & sk, value, data
Next
End If
End Sub
'Waiting for process to finish
Sub WaitForProcess
Const PROCESSNAME = "jre-8u221-windows-i586.exe"
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & PROCESSNAME & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
If iniproc=0 Then
Set cproc=Nothing
Set svc=Nothing
wscript.quit(1)
End If
Do While iniproc = 1
wscript.sleep 5000
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & PROCESSNAME & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
Set cproc=Nothing
Set svc=Nothing
End Sub
Answers (1)
regardig "Unable to wait for process" error - Google is saying it happens e.g. when command starts with a space. I would suggest to display the full uninstallation command to verify it:
[..]
strUninstallString = strPdtCode & " /qn REBOOT=ReallySuppress"
Wscript.Echo "*" &strUninstallString &"*"
[..]
If there is a space, you can use the Trim function ---> strUninstallString = Trim(strPdtCode & " /qn REBOOT=ReallySuppress")
I would do exactly the same for the "Exit Sub" part. You already have there a Wscript.Echo. Why not to extend the message to know when exactly it goes to the ELSE part:
Wscript.Echo "Exit Sub didn't work."
Wscript.Echo "Comparing: *" &strRegData &"* with: *" &CurrentVerx86 &"* and *" &CurrentVerx64 &"*"
Wscript.Echo "StrComp x86 returned: " &StrComp(strRegData,CurrentVerx86)
Wscript.Echo "StrComp x64 returned: " &StrComp(strRegData,CurrentVerx64)
I belivie then you will figure out why it does not work as expected.