|
| 1 | +Const HKEY_CURRENT_USER = &H80000001 |
| 2 | +Const HKEY_LOCAL_MACHINE = &H80000002 |
| 3 | + |
| 4 | +Function RemoveConflictingProducts() |
| 5 | + On Error Resume Next |
| 6 | + |
| 7 | + Dim shell, reg, processed |
| 8 | + Set shell = CreateObject("WScript.Shell") |
| 9 | + Set reg = GetObject("winmgmts:\\.\root\default:StdRegProv") |
| 10 | + Set processed = CreateObject("Scripting.Dictionary") |
| 11 | + |
| 12 | + If Err.Number <> 0 Then |
| 13 | + LogMessage "RemoveConflictingProducts: failed to initialize objects: " & Err.Description |
| 14 | + RemoveConflictingProducts = 3 |
| 15 | + Exit Function |
| 16 | + End If |
| 17 | + |
| 18 | + Dim hives(1), roots(1) |
| 19 | + hives(0) = HKEY_LOCAL_MACHINE |
| 20 | + hives(1) = HKEY_CURRENT_USER |
| 21 | + roots(0) = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" |
| 22 | + roots(1) = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" |
| 23 | + |
| 24 | + Dim hive, root, enumResult, subKeys, i |
| 25 | + Dim foundAny |
| 26 | + foundAny = False |
| 27 | + |
| 28 | + For Each hive In hives |
| 29 | + For Each root In roots |
| 30 | + enumResult = reg.EnumKey(hive, root, subKeys) |
| 31 | + If enumResult = 0 And IsArray(subKeys) Then |
| 32 | + For i = 0 To UBound(subKeys) |
| 33 | + Dim subKeyName, fullPath, displayName |
| 34 | + subKeyName = CStr(subKeys(i)) |
| 35 | + fullPath = root & "\" & subKeyName |
| 36 | + displayName = ReadStringValue(reg, hive, fullPath, "DisplayName") |
| 37 | + |
| 38 | + If IsTargetProduct(displayName) Then |
| 39 | + foundAny = True |
| 40 | + If Not UninstallConflict(reg, shell, hive, fullPath, subKeyName, displayName, processed) Then |
| 41 | + RemoveConflictingProducts = 3 |
| 42 | + Exit Function |
| 43 | + End If |
| 44 | + End If |
| 45 | + Next |
| 46 | + End If |
| 47 | + Next |
| 48 | + Next |
| 49 | + |
| 50 | + If Not foundAny Then |
| 51 | + LogMessage "RemoveConflictingProducts: no conflicting products detected." |
| 52 | + End If |
| 53 | + |
| 54 | + RemoveConflictingProducts = 1 |
| 55 | +End Function |
| 56 | + |
| 57 | +Private Function UninstallConflict(reg, shell, hive, fullPath, subKeyName, displayName, processed) |
| 58 | + UninstallConflict = False |
| 59 | + |
| 60 | + Dim windowsInstaller, quietCmd, uninstallCmd, commandToRun, productCode |
| 61 | + windowsInstaller = ReadDwordValue(reg, hive, fullPath, "WindowsInstaller") |
| 62 | + quietCmd = Trim(ReadStringValue(reg, hive, fullPath, "QuietUninstallString")) |
| 63 | + uninstallCmd = Trim(ReadStringValue(reg, hive, fullPath, "UninstallString")) |
| 64 | + productCode = Trim(subKeyName) |
| 65 | + |
| 66 | + If windowsInstaller = 1 And LooksLikeProductCode(productCode) Then |
| 67 | + commandToRun = BuildMsiUninstallCommand(shell, productCode) |
| 68 | + Else |
| 69 | + If Len(quietCmd) > 0 Then |
| 70 | + commandToRun = quietCmd |
| 71 | + Else |
| 72 | + commandToRun = uninstallCmd |
| 73 | + End If |
| 74 | + |
| 75 | + If Len(commandToRun) = 0 Then |
| 76 | + LogMessage "RemoveConflictingProducts: no uninstall command found for " & displayName |
| 77 | + Exit Function |
| 78 | + End If |
| 79 | + |
| 80 | + If Len(quietCmd) = 0 And (Not CommandHasQuietSwitch(commandToRun)) And (Not CommandTargetsMsiexec(commandToRun)) Then |
| 81 | + commandToRun = commandToRun & " /S" |
| 82 | + End If |
| 83 | + End If |
| 84 | + |
| 85 | + Dim dedupeKey |
| 86 | + dedupeKey = UCase(productCode & "|" & commandToRun) |
| 87 | + If processed.Exists(dedupeKey) Then |
| 88 | + UninstallConflict = True |
| 89 | + Exit Function |
| 90 | + End If |
| 91 | + processed.Add dedupeKey, True |
| 92 | + |
| 93 | + LogMessage "RemoveConflictingProducts: uninstalling " & displayName & " using: " & commandToRun |
| 94 | + |
| 95 | + Dim exitCode |
| 96 | + exitCode = shell.Run(commandToRun, 0, True) |
| 97 | + LogMessage "RemoveConflictingProducts: uninstall exit code for " & displayName & ": " & CStr(exitCode) |
| 98 | + |
| 99 | + If Not IsAcceptedExitCode(exitCode) Then |
| 100 | + LogMessage "RemoveConflictingProducts: uninstall failed for " & displayName |
| 101 | + Exit Function |
| 102 | + End If |
| 103 | + |
| 104 | + UninstallConflict = True |
| 105 | +End Function |
| 106 | + |
| 107 | +Private Function IsTargetProduct(displayName) |
| 108 | + Dim nameUpper |
| 109 | + nameUpper = UCase(Trim(displayName)) |
| 110 | + IsTargetProduct = (Left(nameUpper, 8) = "SUNSHINE") _ |
| 111 | + Or (Left(nameUpper, 6) = "APOLLO") _ |
| 112 | + Or (Left(nameUpper, 9) = "VIBESHINE") |
| 113 | +End Function |
| 114 | + |
| 115 | +Private Function BuildMsiUninstallCommand(shell, productCode) |
| 116 | + Dim msiPath |
| 117 | + msiPath = shell.ExpandEnvironmentStrings("%WINDIR%\System32\msiexec.exe") |
| 118 | + BuildMsiUninstallCommand = Chr(34) & msiPath & Chr(34) _ |
| 119 | + & " /x " & productCode _ |
| 120 | + & " /qn /norestart REBOOT=ReallySuppress SUPPRESSMSGBOXES=1" |
| 121 | +End Function |
| 122 | + |
| 123 | +Private Function LooksLikeProductCode(value) |
| 124 | + Dim trimmed |
| 125 | + trimmed = Trim(value) |
| 126 | + LooksLikeProductCode = (Len(trimmed) = 38) _ |
| 127 | + And (Left(trimmed, 1) = "{") _ |
| 128 | + And (Right(trimmed, 1) = "}") |
| 129 | +End Function |
| 130 | + |
| 131 | +Private Function CommandHasQuietSwitch(commandLine) |
| 132 | + Dim upper |
| 133 | + upper = " " & UCase(commandLine) & " " |
| 134 | + CommandHasQuietSwitch = (InStr(upper, " /S ") > 0) _ |
| 135 | + Or (InStr(upper, " /SILENT ") > 0) _ |
| 136 | + Or (InStr(upper, " /VERYSILENT ") > 0) _ |
| 137 | + Or (InStr(upper, " /QUIET ") > 0) _ |
| 138 | + Or (InStr(upper, " /QN ") > 0) |
| 139 | +End Function |
| 140 | + |
| 141 | +Private Function CommandTargetsMsiexec(commandLine) |
| 142 | + Dim executable |
| 143 | + executable = LCase(GetExecutablePath(commandLine)) |
| 144 | + If Right(executable, 12) = "\msiexec.exe" Then |
| 145 | + CommandTargetsMsiexec = True |
| 146 | + Exit Function |
| 147 | + End If |
| 148 | + If executable = "msiexec.exe" Or executable = "msiexec" Then |
| 149 | + CommandTargetsMsiexec = True |
| 150 | + Exit Function |
| 151 | + End If |
| 152 | + CommandTargetsMsiexec = False |
| 153 | +End Function |
| 154 | + |
| 155 | +Private Function GetExecutablePath(commandLine) |
| 156 | + Dim value, closingQuote, firstSpace |
| 157 | + value = Trim(commandLine) |
| 158 | + If Len(value) = 0 Then |
| 159 | + GetExecutablePath = "" |
| 160 | + Exit Function |
| 161 | + End If |
| 162 | + |
| 163 | + If Left(value, 1) = Chr(34) Then |
| 164 | + closingQuote = InStr(2, value, Chr(34)) |
| 165 | + If closingQuote > 2 Then |
| 166 | + GetExecutablePath = Mid(value, 2, closingQuote - 2) |
| 167 | + Exit Function |
| 168 | + End If |
| 169 | + End If |
| 170 | + |
| 171 | + firstSpace = InStr(value, " ") |
| 172 | + If firstSpace <= 0 Then |
| 173 | + GetExecutablePath = value |
| 174 | + Else |
| 175 | + GetExecutablePath = Left(value, firstSpace - 1) |
| 176 | + End If |
| 177 | +End Function |
| 178 | + |
| 179 | +Private Function IsAcceptedExitCode(code) |
| 180 | + IsAcceptedExitCode = (code = 0) Or (code = 3010) Or (code = 1605) |
| 181 | +End Function |
| 182 | + |
| 183 | +Private Function ReadStringValue(reg, hive, path, valueName) |
| 184 | + On Error Resume Next |
| 185 | + Dim value, rc |
| 186 | + value = "" |
| 187 | + rc = reg.GetStringValue(hive, path, valueName, value) |
| 188 | + If rc <> 0 Or IsNull(value) Then |
| 189 | + value = "" |
| 190 | + rc = reg.GetExpandedStringValue(hive, path, valueName, value) |
| 191 | + End If |
| 192 | + If IsNull(value) Then |
| 193 | + value = "" |
| 194 | + End If |
| 195 | + ReadStringValue = CStr(value) |
| 196 | +End Function |
| 197 | + |
| 198 | +Private Function ReadDwordValue(reg, hive, path, valueName) |
| 199 | + On Error Resume Next |
| 200 | + Dim value, rc |
| 201 | + value = 0 |
| 202 | + rc = reg.GetDWORDValue(hive, path, valueName, value) |
| 203 | + If rc <> 0 Or IsNull(value) Then |
| 204 | + value = 0 |
| 205 | + End If |
| 206 | + ReadDwordValue = CLng(value) |
| 207 | +End Function |
| 208 | + |
| 209 | +Private Sub LogMessage(message) |
| 210 | + On Error Resume Next |
| 211 | + Session.Log message |
| 212 | +End Sub |
0 commit comments