rem @echo off
rem set SAVCleanWipe=con
rem set warn_first=0
rem set debug=1
rem set Detailed_Search=0
rem set APPCode={7C5372DF-8040-43C5-8E54-2034C9745022}

rem *************************************************************************************
rem EnumMSI.bat
rem
rem   This logic takes the MSI APPCode which was set in FindApp.bat and searches the registry
rem   for any related stuff. Ultimately a file called AutoMSIRegs.reg is created which contains
rem   all the entries to be deleted from the registry.  This file will look something like the
rem   following:
rem
rem     REGEDIT4
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Features\5BBC915D3B845D64893627996BD80DD3 ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Products\5BBC915D3B845D64893627996BD80DD3 ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Products\5BBC915D3B845D64893627996BD80DD3\SourceList ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Products\5BBC915D3B845D64893627996BD80DD3\SourceList\Media ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Products\5BBC915D3B845D64893627996BD80DD3\SourceList\Net ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\UpgradeCodes\BF9DACEA0693B5347B6760F9F875BF10 ]
rem     
rem     [-HKEY_CLASSES_ROOT\Installer\Products\5BBC915D3B845D64893627996BD80DD3 ]
rem     
rem     [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D519CBB5-48B3-46D5-9863-7299B68DD03D} ]
rem     
rem     [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Features\5BBC915D3B845D64893627996BD80DD3 ]
rem
rem     etc...
rem
rem   The AutoMSIRegs.reg file is executed to delete the registry keys found.
rem
rem *************************************************************************************

rem Get the product code for the one that was installed/to be removed.

rem This code expects that FindApp.bat has already been called

if "%APPCode%"=="" goto :OtherProdCodes

if %debug%==1 echo APPCode=%APPCode% >> %SAVCleanWipe%
call :DoWork %APPCode%

if "%APPCode2%"=="" goto :OtherProdCodes

if %debug%==1 echo APPCode2=%APPCode2% >> %SAVCleanWipe%
call :DoWork %APPCode2%


:OtherProdCodes
REM Check for all other products.
If not %Detailed_Search%==1 goto :end
if %warn_first%==1 echo.
if %Warn_First%==1 echo Started detailed MSI product code search at %time%
echo Started detailed MSI product code search at %time%>> %SAVCleanWipe%
if exist MSICodesTmp.txt del /q MSICodesTmp.txt
echo.{24BF7A02-B60A-494B-843A-793BBC77DED4}>>MSICodesTmp.txt
echo.{0EFC6259-3AD8-4CD2-BC57-D4937AF5CC0E}>>MSICodesTmp.txt
echo.{68C33091-72FA-4DB8-A40E-8CEAE4552D73}>>MSICodesTmp.txt
echo.{7712ADB6-E855-4116-B9E0-1E23BE4E7CE6}>>MSICodesTmp.txt
echo.{7D2B86CA-2D5D-469E-92ED-E56B62BD1D3C}>>MSICodesTmp.txt
echo.{BD12EB48-DBDF-11D3-BEEA-00A0CC272509}>>MSICodesTmp.txt
echo.{C037167E-B804-4CBD-8D52-7D2A84BE543F}>>MSICodesTmp.txt
echo.{CD05B6DD-65EC-43AC-BE00-73394F19B3AA}>>MSICodesTmp.txt
echo.{D6C64C69-F9F5-11D3-BEEA-00A0CC272509}>>MSICodesTmp.txt
echo.{1D17B4AB-E898-6034-EA78-B87A5A690FDE}>>MSICodesTmp.txt
echo.{D6C64C68-F9F5-11D3-BEEA-00A0CC272509}>>MSICodesTmp.txt
echo.{BD12EB47-DBDF-11D3-BEEA-00A0CC272509}>>MSICodesTmp.txt
echo.{74BE21DB-FDBD-3D11-EBAE-000ACC725290}>>MSICodesTmp.txt
echo.{86C46C6D-5F9F-3D11-EBAE-000ACC725290}>>MSICodesTmp.txt
echo.{0EFC6259-3AD8-4CD2-BC57-D4937AF5CC0E}>>MSICodesTmp.txt
echo.{7D2B86CA-2D5D-469E-92ED-E56B62BD1D3C}>>MSICodesTmp.txt
echo.{5F5A0389-C8D6-49D6-8812-9F7F439E72AC}>>MSICodesTmp.txt
echo.{1FB9B789-CFB6-48CD-A579-1D0A30F36435}>>MSICodesTmp.txt
echo.{848AC794-8B81-440A-81AE-6474337DB527}>>MSICodesTmp.txt
echo.{D1E5AC9D-DA9D-4167-8130-30B59D01F52D}>>MSICodesTmp.txt
echo.{00CD72B3-E2DF-4DFC-BCC1-5CC4F564518D}>>MSICodesTmp.txt
echo.{A6F7E997-2236-4145-A028-438F2484241A}>>MSICodesTmp.txt
echo.{B5FDA445-CAC4-4BA6-A8FB-A7212BD439DE}>>MSICodesTmp.txt
echo.{1F211E59-C268-4A86-ACC2-5B0CD153C26C}>>MSICodesTmp.txt
echo.{3E172636-AE83-474A-9D07-E31C22C6DDC2}>>MSICodesTmp.txt
echo.{790EC520-CCCC-4810-A0FE-061633204CE4}>>MSICodesTmp.txt
echo.{5A633ED0-E5D7-4D65-AB8D-53ED43510284}>>MSICodesTmp.txt
echo.{0DE336A5-7D5E-56D4-BAD8-35DE34152048}>>MSICodesTmp.txt
echo.{390E8423-8825-9AC4-3BBA-116A57EF1A9F}>>MSICodesTmp.txt
echo.{E761730C-408B-DBC4-D825-D7A248EB45F3}>>MSICodesTmp.txt
echo.{20A7FB42-A06B-B494-48A3-97B3CB77ED4D}>>MSICodesTmp.txt
echo.{8C9675B6-ACA7-3434-0830-320159FC19B2}>>MSICodesTmp.txt
echo.{3267A21D-91BA-CFF4-9953-F3D6DF226267}>>MSICodesTmp.txt
echo.{C7ACBA3A-7F0D-B1B4-88CC-E88A028AF713}>>MSICodesTmp.txt
echo.{D905F463-E053-A464-699D-581AA85F8026}>>MSICodesTmp.txt
echo.{8BABD476-E21A-EAD4-68CB-71205C7A9E91}>>MSICodesTmp.txt
echo.{6BDA2177-558E-6114-9B0E-E132EBE4C76E}>>MSICodesTmp.txt
echo.{9526CFE0-8DA3-2DC4-CB75-4D39A75FCCE0}>>MSICodesTmp.txt
echo.{96C46C6D-5F9F-3D11-EBAE-000ACC725290}>>MSICodesTmp.txt
echo.{3248E093-5288-4CA9-B3AB-11A675FEA1F9}>>MSICodesTmp.txt

for /f "Delims=$" %%a in (MSICodesTmp.txt) do call :DoWork %%a
if %warn_first%==1 echo.
if %warn_first%==1 echo Finished detailed MSI product code search at %time%
echo Finished detailed MSI product code search at %time%>> %SAVCleanWipe%
if %warn_first%==1 echo.
if exist MSICodesTmp.txt del /q MSICodesTmp.txt
goto :end

:DoWork
set APPCodeX=%1
if exist CnvrtMSICode.txt del /q CnvrtMSICode.txt
esugMSIConvert %APPCodeX% > CnvrtMSICode.txt
if not exist CnvrtMSICode.txt echo Invalid MSI code.>> %SAVCleanWipe% & goto :end
set CnvrtMSICode=
for /f "delims=$" %%a in (CnvrtMSICode.txt) do set CnvrtMSICode=%%a
if exist CnvrtMSICode.txt del /q CnvrtMSICode.txt
if "%CnvrtMSICode%"=="" echo Invalid MSI code.>> %SAVCleanWipe% & goto :end

if exist UpgradeMSICode.txt del /q UpgradeMSICode.txt
set UpgradeMSICode=
esugreg HKLM %APPCodeX% -getmsiupgradecode > UpgradeMSICode.txt
for /f "delims=$" %%a in (UpgradeMSICode.txt) do set UpgradeMSICode=%%a
if exist UpgradeMSICode.txt del /q UpgradeMSICode.txt

if %debug%==1 echo APPCodeX=%AppCodeX% >> %SAVCleanWipe%
if %debug%==1 echo CnvrtMSICode=%CnvrtMSICode% >> %SAVCleanWipe%
if %debug%==1 echo UpgradeMSICode=%UpgradeMSICode% >> %SAVCleanWipe%

for %%a in (CnvrtMSICode.txt AutoMSIRegHKCR.txt AutoMSIRegHKLM.txt AutoMSIRegs.reg) do if exist %%a del /q %%a

set APPCodeX=%APPCodeX:{=%
set APPCodeX=%APPCodeX:}=%
set NoDash=
Set NoDash=%APPCodeX:-=%
set CnvrtMSICode=%CnvrtMSICode:{=%
set CnvrtMSICode=%CnvrtMSICode:}=%

if %debug%==1 echo NoDash=%NoDash% >> %SAVCleanWipe%

REM Search for MSI Codes in Key Name.
echo --------------- 1. Searching registry for MSI Product Code %APPCodeX% in the Key name.>> %SAVCleanWipe%
esugreg HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -search %APPCodeX% -hide -key >> AutoMSIRegHKLM.txt
esugreg HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\App Management\ARPCache" -search %APPCodeX% -hide -key >> AutoMSIRegHKLM.txt

echo --------------- 2. Searching registry for MSI Product Code %CnvrtMSICode% in the Key name.>> %SAVCleanWipe%
esugreg HKCR "Installer" -search %CnvrtMSICode% -hide -key >> AutoMSIRegHKCR.txt
esugreg HKLM "SOFTWARE\Classes\Installer" -search %CnvrtMSICode% -hide -key >> AutoMSIRegHKLM.txt
esugreg HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData" -search %CnvrtMSICode% -hide -key >> AutoMSIRegHKLM.txt

echo --------------- 3. Searching registry for MSI Product Code %UpgradeMSICode% in the Key name.>> %SAVCleanWipe%
esugreg HKCR "Installer\UpgradeCodes" -search %UpgradeMSICode% -hide -key >> AutoMSIRegHKCR.txt
esugreg HKLM "SOFTWARE\Classes\Installer\UpgradeCodes" -search %UpgradeMSICode% -hide -key >> AutoMSIRegHKLM.txt
esugreg HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes" -search %UpgradeMSICode% -hide -key >> AutoMSIRegHKLM.txt

echo --------------- 4. Searching registry for MSI Product Code %CnvrtMSICode% in the value name.>> %SAVCleanWipe%
esugreg HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData" -search %CnvrtMSICode% -hide -name >> AutoMSIRegHKLM.txt


:StartDeciding
echo.REGEDIT4>>AutoMSIRegs.reg
echo.>>AutoMSIRegs.reg

echo --------------- Selecting entries found in HKCR>> %SAVCleanWipe%
rem copy AutoMSIRegHKCR.txt AutoMSIRegHKCR.1.txt
for /f "delims=$" %%a in (AutoMSIRegHKCR.txt) do call :CreateMSIRM2 HKEY_CLASSES_ROOT "%%a"

echo --------------- Selecting entries found in HKLM>> %SAVCleanWipe%
rem copy AutoMSIRegHKLM.txt AutoMSIRegHKLM.1.txt
for /f "delims=$" %%a in (AutoMSIRegHKLM.txt) do call :CreateMSIRM2 HKEY_LOCAL_MACHINE "%%a"

goto :DelAutoMSI

:CreateMSIRM2
set OneLine=
set OneLine=%2
if %OneLine%=="" goto :eof
if %OneLine%==" " goto :eof
set OneLine=%OneLine:"=%

echo %OneLine% | find /i "Key:" >NUL
if not %errorlevel%==0 goto :eof
set KeyLine=%OneLine:Key: =%

rem echo KeyLine==%KeyLine% >> %SAVCleanWipe%

echo.[-%1\%KeyLine%]>>AutoMSIRegs.reg
echo.>>AutoMSIRegs.reg

goto :eof


:DelAutoMSI
echo --------------- Removing registry entries selected for MSI product code %APPCodeX%.>> %SAVCleanWipe%
copy "AutoMSIRegs.reg" "logs\Registry_Entries_Deleted_per_MSICode_%APPCodeX%.txt" >NUL
ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"AutoMSIRegs.reg"
if %OS_64%==1 (
	ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"AutoMSIRegs.reg" /WOW6432
)
for %%a in (AutoMSIRegHKCR.txt AutoMSIRegHKLM.txt AutoMSIRegs.reg) do if exist %%a del /q %%a
goto :eof

:end
for %%a in (MSICodesTmp.txt CnvrtMSICode.txt AutoMSIRegHKCR.txt AutoMSIRegHKLM.txt AutoMSIRegs.reg) do if exist %%a del /q %%a
