|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
I have a problem where I periodically have to rollback many machines on the network to an older version of our software (distributed as MSI package).
Using the code
The MSIUninstaller.exe call the Microsoft MsiExec.exe to handle the actual uninstallation of the MSI package. This function show the primary functionality of the program:
Sub Process(ByVal DisplayName As String, ByVal bUninstall As Boolean, ByVal bDisplay As Boolean, ByVal bQuiet As Boolean, ByVal bLog As Boolean, ByVal strSkip As String)
' Retrieve all the subkeys for the specified key.
Dim names As String() = m_rKey.GetSubKeyNames
Dim version As String = ""
Dim publisher As String = ""
Dim dispName As String = ""
Dim iFound As Integer
For Each s As String In names
dispName = GetKeyValue(s, "DisplayName")
If dispName <> "" Then
If WildCardCompare(dispName, DisplayName) Then
version = GetKeyValue(s, "DisplayVersion")
publisher = GetKeyValue(s, "Publisher")
If strSkip <> "none" And WildCardCompare(version, strSkip) Then
Continue For
End If
Dim cmd As String = Environment.SystemDirectory.ToString + "\MsiExec.exe /x " + s
If bQuiet Then
cmd += " /qn"
End If
If bLog Then
cmd += " /lie+c:\MSIUninstaller.log.txt"
End If
If Not bDisplay Then
Console.WriteLine("Publisher = " + publisher)
Console.WriteLine("DisplayName = " + dispName)
Console.WriteLine("version = " + version)
Console.WriteLine("GUID = " + s)
Console.WriteLine("EsitmatedSize = " + (CDbl(GetKeyValue(s, "EstimatedSize")) / 1024).ToString("N2") + " Mb")
Dim dato As String = GetKeyValue(s, "InstallDate")
dato = dato.Substring(6) + "-" + dato.Substring(4, 2) + "-" + dato.Substring(0, 4)
Console.WriteLine("InstallDate = " + CDate(dato).ToString("dd-MMM-yyy"))
Console.WriteLine("InstallSource = " + GetKeyValue(s, "InstallSource") + vbLf)
End If
If bUninstall Then
Shell(cmd)
End If
iFound += 1
End If
End If
Next s
If Not bQuiet And iFound = 0 Then
Console.WriteLine(DisplayName + " was not found in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall on this computer" + vbLf)
End If
End Sub
Private Function GetKeyValue(ByVal keyName As String, ByVal valueName As String) As String
Dim obj As Object = m_rKey.OpenSubKey(keyName).GetValue(valueName)
If Not obj Is Nothing Then
Return obj
Else
Return ""
End If
End Function
Private Function WildCardCompare(ByVal strInput As String, ByVal strWildCard As String) As Boolean
strWildCard = "^" + Regex.Escape(strWildCard).Replace("\*", ".*").Replace("\?", ".") + "$"
Return Regex.IsMatch(strInput, strWildCard, RegexOptions.IgnoreCase)
End Function
|
|||||||||||||||||||||||||||||||||||