Skip to content

Instantly share code, notes, and snippets.

@VertigoRay
Last active December 14, 2015 13:09
Show Gist options
  • Save VertigoRay/5091728 to your computer and use it in GitHub Desktop.
Save VertigoRay/5091728 to your computer and use it in GitHub Desktop.
This will Query WMI for the specified program and uninstall it with Windows Installer - which covers about 90% of our supported applications. All actions are logged to %SystemRoot%\Logs\SCCM.
Option Explicit
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Usage:
' cscript.exe Uninstaller.vbs ApplicationName [LogFileName]
'
' Parameters:
' ApplicationName The Product Name of the Program you want to Uninstall. This will
' remove all instances of the program, so be as specific as you want.
' - Example: "Adobe" would remove all Adobe products (anything with Adobe in the Name)
' - Example: "Adobe Reader" will remove all versions of Adobe Reader.
'
' LogFileName [Optional] The Name of the Log File (DO NOT INCLUDE THE .log EXTENSION)
' stored in %SystemRoot%\Logs. All log files from this VBS are appended with:
' .Uninstaller.log
' - If not supplied, the ApplicationName will be used.
'
' Description:
' This will Query WMI for Programs installed with Windows Installer - which covers about 90%
' of our supported applications. All actions are logged to %SystemRoot%\Logs.
'
' Example:
' cscript.exe Uninstaller.vbs "Adobe Reader"
'
' Example:
' cscript.exe Uninstaller.vbs "Adobe Reader" "Adobe Reader - Install"
'
' Example for CAS:
' cscript.exe //E:vbscript Uninstaller.vbs "Adobe Reader" "Adobe Reader - Install"
'
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim DeploymentTypeName
If WScript.Arguments.Count = 0 Then
WScript.Echo "Usage: cscript.exe Uninstaller.vbs ""Adobe Reader""[ ""Log File Name (w/out extension)""]"
WScript.Quit
End If
Dim strProgram, objShell, objFSO, logFolder, logFile, objLog, strComputer, objWMIService, colItems, objItem
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strProgram = WScript.Arguments.Item(0)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Sub CreateFolderTree(strTempPath)
If Not objFSO.FolderExists(objFSO.GetParentFolderName(strTempPath)) Then
CreateFolderTree(objFSO.GetParentFolderName(strTempPath))
End If
If Not objFSO.FolderExists(strTempPath) Then
objFSO.CreateFolder(strTempPath)
End If
End Sub
logFolder = objShell.ExpandEnvironmentStrings( "%SystemRoot%\Logs\" )
CreateFolderTree(logFolder)
If WScript.Arguments.Count > 1 Then
logFile = objShell.ExpandEnvironmentStrings( logFolder & WScript.Arguments.Item(1) &".Uninstaller.log" )
Else
logFile = objShell.ExpandEnvironmentStrings( logFolder & strProgram &".Uninstaller.log" )
End If
Set objLog = objFSO.OpenTextFile(logFile, ForWriting, True)
Function LogIt_action (logLine, noPrefix)
' [string] logLine is logged.
' [bool] noPrefix
' - Removes the Prefix. Default False.
' - Prefix used in Install/Uninstall steps only.
Dim t, y, m, d, h, i, s, ts, pre, logThis
t = Timer/3600
y = DatePart("yyyy",Date)
m = Right("0" & DatePart("m",Date), 2)
d = Right("0" & DatePart("d",Date), 2)
h = Right("0" & Int(t), 2)
i = Right("0" & Int((t - h) * 60), 2)
s = Right("0" & Int((((t - h) * 60 - m) * 60)*1000)/1000, 6)
ts = "["& y &"-"& m &"-"& d &" "& h &":"& i &":"& s &"] "
If noPrefix Then
pre = ""
Else
pre = "+++ "
End If
WScript.Echo(ts & pre & logLine)
objLog.WriteLine(ts & pre & logLine)
End Function
Function LogIt (logLine)
LogIt_action logLine, False
End Function
Function logItNoPrefix (logLine)
LogIt_action logLine, True
End Function
Dim CurrentDirectory, ProcessorArchitecture
logItNoPrefix "~~~~~ BEGIN: "& strProgram
logItNoPrefix "Log File: "& logFile
CurrentDirectory = objFSO.GetParentFolderName(WScript.ScriptFullName)
logItNoPrefix "CurrentDirectory: "& CurrentDirectory
ProcessorArchitecture = objShell.ExpandEnvironmentStrings( "%PROCESSOR_ARCHITECTURE%" )
logItNoPrefix "ProcessorArchitecture: "& ProcessorArchitecture
logItNoPrefix "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LogIt "Querying for Program: "& strProgram
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Product WHERE Name like '%" & strProgram & "%'",,48)
LogIt "Query Complete. Handling results, if any ..."
For Each objItem in colItems
logItNoPrefix "AssignmentType: "& objItem.AssignmentType
logItNoPrefix "Caption: "& objItem.Caption
logItNoPrefix "Description: "& objItem.Description
logItNoPrefix "IdentifyingNumber: "& objItem.IdentifyingNumber
logItNoPrefix "InstallDate: "& objItem.InstallDate
logItNoPrefix "InstallDate2: "& objItem.InstallDate2
logItNoPrefix "InstallLocation: "& objItem.InstallLocation
logItNoPrefix "InstallState: "& objItem.InstallState
logItNoPrefix "HelpLink: "& objItem.HelpLink
logItNoPrefix "HelpTelephone: "& objItem.HelpTelephone
logItNoPrefix "InstallSource: "& objItem.InstallSource
logItNoPrefix "Language: "& objItem.Language
logItNoPrefix "LocalPackage: "& objItem.LocalPackage
logItNoPrefix "Name: "& objItem.Name
logItNoPrefix "PackageCache: "& objItem.PackageCache
logItNoPrefix "PackageCode: "& objItem.PackageCode
logItNoPrefix "PackageName: "& objItem.PackageName
logItNoPrefix "ProductID: "& objItem.ProductID
logItNoPrefix "RegOwner: "& objItem.RegOwner
logItNoPrefix "RegCompany: "& objItem.RegCompany
logItNoPrefix "SKUNumber: "& objItem.SKUNumber
logItNoPrefix "Transforms: "& objItem.Transforms
logItNoPrefix "URLInfoAbout: "& objItem.URLInfoAbout
logItNoPrefix "URLUpdateInfo: "& objItem.URLUpdateInfo
logItNoPrefix "Vendor: "& objItem.Vendor
logItNoPrefix "WordCount: "& objItem.WordCount
logItNoPrefix "Version: "& objItem.Version
LogIt "Uninstalling ..."
Dim execThis, objExec
execThis = "msiexec /qn /x " & objItem.IdentifyingNumber & " /log """& Replace(logFile, ".log", "."&objItem.Name &".msi.log") &""""
logItNoPrefix "Executing: "& execThis
Set objExec = objShell.Exec(execThis)
Do
logItNoPrefix "> "& objExec.StdOut.ReadLine()
Loop While Not objExec.StdOut.atEndOfStream
Do While objExec.Status = 0
WScript.Sleep 100
Loop
logItNoPrefix ">>> ("& objExec.ProcessID &") ExitCode: "& objExec.ExitCode
' If runResult = 2147549445 then
' LogIt objExec.ReturnValue &": RPC Server Fault Error"
' WScript.Quit(objExec.ReturnValue)
' elseif runResult > 0 then
' LogIt "Trying to Uninstall a different way ..."
' Set objExec = objWMIService.ExecMethod("Win32_Product.IdentifyingNumber='" & objItem.IdentifyingNumber & "',Name='" & objItem.Name & "',Version='" & objItem.Version & "'", "Uninstall")
' LogIt "OutParam: " & objExec.ReturnValue
' iF objExec.ReturnValue = 2147549445 then
' LogIt objExec.ReturnValue &": RPC Server Fault Error"
' WScript.Quit(objExec.ReturnValue)
' elseif objExec.ReturnValue > 0 then
' LogIt objExec.ReturnValue &": "& Err.Description
' WScript.Quit(objExec.ReturnValue)
' end if
' end if
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment