Uninstall Windows Applications Using VBScript
-
2012年2月20日 12:59
Hi guys,
Following on from this thread:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/bef4b845-ef77-4882-afe1-a479c4531b03/
It was never answered how to actually use the given Uninstall ID to uninstall the product.
Working on this script:
const HKEY_CLASSES_ROOT = &H80000000 const HKEY_CURRENT_USER = &H80000001 const HKEY_LOCAL_MACHINE = &H80000002 const HKEY_USERS = &H80000003 const HKEY_CURRENT_CONFIG = &H80000004 const HKEY_DYN_DATA = &H80000005 strComputer = "." set oWsh = createobject("wscript.shell") Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\default:StdRegProv") strKeyPath = "software\microsoft\windows\currentversion\uninstall" ' Root level oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys sMatch = "Adobe" For Each subkey In arrSubKeys on error resume next sDisplayName = oWsh.Regread("HKLM\" & strKeyPath & "\" & subkey & "\DisplayName") if err.number = 0 then on error goto 0 if instr(sDisplayName, sMatch) > 0 then On Error Resume Next sUninstall = oWsh.Regread("HKLM\" & strKeyPath & "\" & subkey & "\UnInstallString") If Err.Number = 0 Then wsh.echo subkey, vbCRLF, sDisplayName, vbCRLF, sUnInstall, vbCRLF, vbCRLF Else wsh.echo subKey, vbCrLf, sDisplayName, vbCrLf, "<None>", vbCrLf, vbCrLf End If On Error GoTo 0 end if end if Next on error goto 0How would I then take these values and get them to actually unintall the products?
Help would be much appreciated! thanks!Phill
全部回复
-
2012年2月20日 13:05
The UninstallString will contain the command that you need to run to uninstall the application:
like MsiExec.exe /I{03AC245F-4C64-425C-89CF-7783C1D3AB2C} for example.
edit: to run the command in your vbscript script use something like
Set objShell = Wscript.CreateObject("Wscript.Shell")
sCmd = sUninstall
intReturn = objShell.run(sCmd,0,true)
- 已编辑 Wilfred Heap 2012年2月20日 13:12
- 已编辑 Wilfred Heap 2012年2月20日 13:13
- 已编辑 Wilfred Heap 2012年2月20日 13:16
- 已标记为答案 IamMredMicrosoft Employee, Owner 2012年2月25日 20:19
-
2012年2月20日 13:25版主
You just need to Run the sUnInstall string returned from the RegRead as a command, maybe something like this (using cscript at a command prompt or in a shortcut) ...
const HKEY_CLASSES_ROOT = &H80000000 const HKEY_CURRENT_USER = &H80000001 const HKEY_LOCAL_MACHINE = &H80000002 const HKEY_USERS = &H80000003 const HKEY_CURRENT_CONFIG = &H80000004 const HKEY_DYN_DATA = &H80000005 strComputer = "." set oWsh = createobject("wscript.shell") Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\default:StdRegProv") strKeyPath = "software\microsoft\windows\currentversion\uninstall" ' Root level oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys sMatch = "Adobe" For Each subkey In arrSubKeys on error resume next sDisplayName = oWsh.Regread("HKLM\" & strKeyPath & "\" & subkey & "\DisplayName") if err.number = 0 then on error goto 0 if instr(sDisplayName, sMatch) > 0 then On Error Resume Next sUninstall = oWsh.Regread("HKLM\" & strKeyPath & "\" & subkey & "\UnInstallString") If Err.Number = 0 Then wsh.echo subkey, vbCRLF, sDisplayName, vbCRLF, sUnInstall wsh.stdout.write "Uninstall this application (y,n)? " if lcase(wsh.stdin.read(1)) = "y" then oWsh.run sUninstall, 1, false else wsh.echo "Procedure aborted." end if Else wsh.echo subKey, vbCrLf, sDisplayName, vbCrLf, "<None>" End If On Error GoTo 0 end if end if NextThe script does not wait for the uninstall to be completed, because the MSI executable usually launches another thread that effectively disconnects the calling thread from the process. You can try it with the third argument in the Run statement set as True, but I doubt it will make much difference.
I also set it to ask for permission before removing the matched application or converted to MsgBox() requests to convert it to a GUI based application. That test can be removed as can all of the WSH.ECHO prompts, if you want it to run silently. These changes are left to the student ;-)
Tom Lavedas
- 已标记为答案 IamMredMicrosoft Employee, Owner 2012年2月25日 20:19
-
2012年2月20日 18:44
Just a quick note:
You can use the MSI object to execute an uninstall via COM. This will allow you to poll the COM object to see when it has been deactivated.
The COM interface to MSI also allows use to search by product name and return the uninstall reference. It is simple to fidnd teh object and just execute the Uninstall method.
Unfortunately I do not have much example code but there are many in the MSI SDK andon the Internet.
Here is one quick sample that I have used.
runit "E:\Projects\Downloads\MBSASetup-x86-EN.msi" WScript.Echo "done" sub runit(package) Dim ret, oInstaller Set oInstaller = CreateObject("WindowsInstaller.Installer","omega2") oInstaller.UILevel=2 for each product in oInstaller.Products wscript.Echo product.State Next 'oInstaller.InstallProduct package,"REMOVE=ALL" 'oInstaller.InstallProduct package,"ACTION=INSTALL" End SubNote that by uncommenting the lines I can switch the mode. This could bedone programmtically too.
The installer can also search by name.
The SDK has four comprehensive examples in VBScript:
ListProducts.vbs
WiLstPrd.vbs
WiLstPrd2.vbs
WiLstPrdEx.vbsThere are other examples and teh documentation is very good.
¯\_(ツ)_/¯
- 已标记为答案 IamMredMicrosoft Employee, Owner 2012年2月25日 20:19

