Magic Uninstall VBS

Discussion in 'Scripts' started by TNeumann, Apr 28, 2005.

  1. TNeumann SR Technical Acct Manager

    I found this on the Altiris Forums & it looked worth taking a look at.

    I use this script to uninstall programs based on the name they have in Add/Remove Programs (actually the value of HKLM\Software\Microsoft\Windows\CurrentVersion\{clisid}\DisplayName)
    since it uses the built in "Uninstall string" it doesn't matter how it was packaged.

    It works best with MSIs though becasue it changes the /I to /x and defaults to msi command line options, but you can set them up however you want.

    EDIT: Please drop me a line at dolan@ctc.com if you find this helpful, and please post any improvements that you have.

    Thanks!

    --------------------------------------------------------------------------
    '------------------------------------------------------------------------------
    ' magic_uninstall.vbs
    '------------------------------------------------------------------------------
    ' generic uninstaller script to uninstall applications based on the Add/Remove
    ' Programs DisplayName and UninstallString properties
    '------------------------------------------------------------------------------
    ' Author: David M. Dolan
    ' Created: 4/19/2005
    ' Modified: 4/19/2005
    '------------------------------------------------------------------------------
    ' Use this script for whatever you want -- I'm stating that there is no warranty
    ' and that I'm not responsible for any damage you do with it.
    '------------------------------------------------------------------------------
    ' designed for use with MSI's but it should work on any other program that
    ' specifies and uninstall string -- but you have to miff out the /Options
    ' (" " works)
    '------------------------------------------------------------------------------


    'On error resume next

    '------------------------------------------------------------------------------
    ' constants
    '------------------------------------------------------------------------------
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const KeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"

    'modify this if you want to, I default mine for MSIs, and if you're silly enough
    ' not to pass your own options and accidentally uninstall something, I want
    ' the dialog to pop up telling you what you just foobarred
    const defaultOptions = "/qb+"
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' read the command line paramters, and bomb out if there is a problem
    '------------------------------------------------------------------------------
    progToRemove = Wscript.Arguments.Named("Program")

    MSIOptions = Wscript.Arguments.Named("Options")
    if MSIOptions = "" then
    MSIOptions = defaultOptions
    end if


    '------------------------------------------------------------------------------
    ' Main Program
    '------------------------------------------------------------------------------
    if progToRemove = "" then
    usage
    else

    SeekAndDestroy

    end if
    Wscript.Quit(0)

    '------------------------------------------------------------------------------
    sub usage
    '------------------------------------------------------------------------------
    Wscript.Echo "usage: magic_uninstall.vbs /Program:<DisplayNameString> /Options:<commandlineopts>"
    Wscript.Echo " ex: magic_uninstall.vbs /Program:""Orca"" /Options:""/qb-"""
    Wscript.Quit(1)
    end sub
    '------------------------------------------------------------------------------
    sub SeekAndDestroy
    '------------------------------------------------------------------------------

    set locator = CreateObject("WbemScripting.SWbemLocator")
    set oWMI = locator.ConnectServer(".","root/default")


    set objReg = oWMI.Get("StdRegProv")

    lRC = objReg.EnumKey (HKEY_LOCAL_MACHINE, KeyPath, arrSubKeys)


    'ok, so we're at the install key, loop through the sub keys...
    For each Subkey in arrSubKeys

    'look at the next key
    newKeyPath = KeyPath & "\" & SubKey

    'get all of the values and store them in two arrays -- value names in arrEntryNames
    ' -- value Types in to arrValueTypes
    objReg.EnumValues HKEY_LOCAL_MACHINE,_
    newKeyPath,arrEntryNames,arrValueTypes

    'make sure we have an array, then grok it
    if isArray(arrEntryNames) then

    uninstallable = "f"

    'loop through the entry names and keep the ones we're looking for
    for i = 0 to Ubound(arrValueTypes)
    entryName = arrEntryNames(i)

    select case entryName

    case "DisplayName"

    'ok, display name, so what's the value? put it into sName
    objReg.GetStringValue HKEY_LOCAL_MACHINE, _
    newKeyPath, entryName, sName

    case "DisplayVersion"
    'ok, displayVersion, so store it into sVers

    objReg.GetStringValue HKEY_LOCAL_MACHINE, _
    newKeyPath, entryName, sVers

    case "UninstallString"
    objReg.GetStringValue HKEY_LOCAL_MACHINE, _
    newKeyPath, entryName, sUninstall
    uninstallable = "t"
    end select

    Next '-- value

    if sName <> "" and uninstallable = "t" then

    'ok so we know that we have a software name, and it's uninstallable, so
    ' only now do we check to see if it's what we're looking for...

    if sName = progToRemove then

    'this is where the magic happens
    set oCmd = CreateObject("Wscript.Shell")

    uninstallCommand = Replace(sUninstall, "/I", "/x") & " " & MSIOptions

    commandLine = "%comspec% /c " & uninstallCommand

    'wscript.echo commandLine

    oCmd.Run commandLine, 0, true

    Wscript.Quit(0) ' haha we're done, quit digging in the registry now!

    end if

    end if

    end if

    sName = ""
    sVers = ""

    Next '-- subKey

    end sub
  2. Nick Altiris/AppSense Guru

    Thanks for bringing this over Tim.
  3. dave.dolan the ultimate script kiddie

    checking in

    Hey guys, I'm flattered to see this has been posted here. This one turned out to help me out of a serious jam. Let me know if you have any questions.


    I noticed that there is a typo in that one, the constant line at the top shouldn't have a space between the two letter Ls at the end in the word "Uninstall"

    --dave
  4. TNeumann SR Technical Acct Manager

    Dave,
    I was gonna steal it for my own :p Has definitely helped me out as well, that's why I brought it over...hope you don't mind.
    I went to edit the vbs text to remove the extra space, it shows up fine in the edit screen.
    Where are you in PA?...not many of us around

    Tim
  5. dave.dolan the ultimate script kiddie

    yeah Johnstown PA, about an hour or so from Pittsburgh. Alteeerse is how people around here say it, and it's like a dirty word to my management. I'm working very hard to change it. In rural PA we still think digital watches are a pretty neat idea.
  6. Nick Altiris/AppSense Guru

    Dave,

    Welcome to the forums and if there is anything we can do to help you convince your management that Altiris isn't the Anti Christ let us know.

    Also, don't be scared off by the fact that Tim is up there in PA with you. He's really not a bad guy once you get to know him, heck he even convinced some woman to marry him this year. ;)
  7. TNeumann SR Technical Acct Manager

    Johnstown really isn't part of PA. You have PA & Western PA lolol

    I had to convince her AND pay her.
  8. Nick Altiris/AppSense Guru

    The interesting thing is that sentence is past tense. Being married myself I am inclined to believe that the paying NEVER stops. ;)
  9. gdellaccio New Member

    Magic Uninstall Packaging Question

    I have successfully configured this useful script, but am having trouble packaging it through SWD to execute on a client test machine. I am not a scripting expert, but am looking for a good way to uninstall programs via NS. The script file gets to the machine fine, but does not execute automatically. I suspect I am not using a good command line to execute it, or not packaging it correctly.
    What am I missing??? Is there a best practice to packaging and executing scripts in the NS?
    We are using CMS level I (not Wise solution).

    Thanks
  10. Mike Altiris Engineer (Super Mod)

    Put the vbs in a folder that you identify as a "software delivery package" and specify a "Program" of cscript.exe vbsname.vbs

    Remember that windows only knows how to execut a .com, .bat, or .exe. Anything else needs the help of one of these to get going.

Share This Page