Wednesday, October 26, 2005

Uninstalling a product from command line.

It is surprising that once you install a program, the only way to remove it is through the add-remove programs unless you have access to the msi that installed it. There is no builtin command line equivalent and this becomes a pain when u r repeatedly installing successive builds of the same product. Luckily, the info required is exposed through WMI and python has hooks into the same (Tested only on xp-sp2. Your mileage may vary...):

import win32com.client
import os

def GetWMI(comp, namespace):
comObj = win32com.client.Dispatch("WbemScripting.SWbemLocator")
return comObj.ConnectServer(comp,namespace)

def GetProductId(productName):
wmi = GetWMI(".", r"root\cimv2")
prods = wmi.ExecQuery("Select * from Win32_Product where Name='%(productName)s'"%vars())
if (prods.Count == 0):
return None
else:
return prods[0].IdentifyingNumber


if __name__=="__main__":
prodCode = GetProductId("My Product Name")
if (prodCode):
print "found a previous installation...."
print "uninstalling..."
os.system("msiexec /x " + prodCode)
else:
print "No previous installation found."

There is a WindowsInstaller.Installer automation object exposed, but I could not get it to work through python :(, guess I have to learn vbscript or some such for sake of efficiency.

No comments: