From 91e788ebb5eaf72deedee5be14ffcb7b7ed47e9f Mon Sep 17 00:00:00 2001 From: GAJ-san Date: Thu, 5 Jan 2023 15:37:30 +0100 Subject: [PATCH] Multiple App uninstall in Mod Template/Functions --- Winget-AutoUpdate/mods/_AppID-template.ps1 | 4 +- Winget-AutoUpdate/mods/_Mods-Functions.ps1 | 129 +++++++++++---------- 2 files changed, 68 insertions(+), 65 deletions(-) diff --git a/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Winget-AutoUpdate/mods/_AppID-template.ps1 index e7d942f..07ac85b 100644 --- a/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -6,8 +6,8 @@ $Proc = @("") $Wait = @("") #Beginning of App Name string to Silently Uninstall (MSI/NSIS/INNO/EXE with defined silent uninstall in registry) -#Required wildcard (*) after, search is done with "-like"! -$App = "" +#Multiple: "app1*","app2*", required wildcard (*) after; search is done with "-like"! +$App = @("") #Beginning of Desktop Link Name to Remove - optional wildcard (*) after, without .lnk, multiple: "lnk1","lnk2" $Lnk = @("") diff --git a/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index 569253e..96c5c79 100644 --- a/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -15,63 +15,9 @@ function Wait-ModsProc ($Wait) { Return } function Uninstall-ModsApp ($App) { - $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" - foreach ($obj in $InstalledSoftware){ - if ($obj.GetValue('DisplayName') -like $App) { - $UninstallString = $obj.GetValue('UninstallString') - $CleanedUninstallString = $UninstallString.Trim([char]0x0022) - if ($UninstallString -like "MsiExec.exe*") { - $ProductCode = Select-String "{.*}" -inputobject $UninstallString - $ProductCode = $ProductCode.matches.groups[0].value - #MSI x64 Installer - $Exec = Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/x$ProductCode REBOOT=R /qn" -PassThru -Wait - #Stop Hard Reboot (if bad MSI!) - if ($Exec.ExitCode -eq 1641) { - Start-Process "C:\Windows\System32\shutdown.exe" -ArgumentList "/a" - } - } - else { - $QuietUninstallString = $obj.GetValue('QuietUninstallString') - if ($QuietUninstallString) { - $QuietUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $QuietUninstallString - $Command = $QuietUninstallString.matches.groups[1].value - $Parameter = $QuietUninstallString.matches.groups[2].value - #All EXE x64 Installers (already defined silent uninstall) - Start-Process $Command -ArgumentList $Parameter -Wait - } - else { - if ((Test-Path $CleanedUninstallString)) { - $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" - } - if ($NullSoft) { - #NSIS x64 Installer - Start-Process $UninstallString -ArgumentList "/S" -Wait - } - else { - if ((Test-Path $CleanedUninstallString)) { - $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" - } - if ($Inno) { - #Inno x64 Installer - Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait - } - else { - Write-Host "x64 Uninstaller unknown, trying the UninstallString from registry..." - $NativeUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $UninstallString - $Command = $NativeUninstallString.matches.groups[1].value - $Parameter = $NativeUninstallString.matches.groups[2].value - #All EXE x64 Installers (native defined uninstall) - Start-Process $Command -ArgumentList $Parameter -Wait - } - } - } - } - $x64 = $true - break - } - } - if (!$x64) { - $InstalledSoftware = Get-ChildItem "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + foreach ($process in $Proc) + { + $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" foreach ($obj in $InstalledSoftware){ if ($obj.GetValue('DisplayName') -like $App) { $UninstallString = $obj.GetValue('UninstallString') @@ -79,7 +25,7 @@ function Uninstall-ModsApp ($App) { if ($UninstallString -like "MsiExec.exe*") { $ProductCode = Select-String "{.*}" -inputobject $UninstallString $ProductCode = $ProductCode.matches.groups[0].value - #MSI x86 Installer + #MSI x64 Installer $Exec = Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/x$ProductCode REBOOT=R /qn" -PassThru -Wait #Stop Hard Reboot (if bad MSI!) if ($Exec.ExitCode -eq 1641) { @@ -92,7 +38,7 @@ function Uninstall-ModsApp ($App) { $QuietUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $QuietUninstallString $Command = $QuietUninstallString.matches.groups[1].value $Parameter = $QuietUninstallString.matches.groups[2].value - #All EXE x86 Installers (already defined silent uninstall) + #All EXE x64 Installers (already defined silent uninstall) Start-Process $Command -ArgumentList $Parameter -Wait } else { @@ -100,7 +46,7 @@ function Uninstall-ModsApp ($App) { $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" } if ($NullSoft) { - #NSIS x86 Installer + #NSIS x64 Installer Start-Process $UninstallString -ArgumentList "/S" -Wait } else { @@ -108,23 +54,80 @@ function Uninstall-ModsApp ($App) { $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" } if ($Inno) { - #Inno x86 Installer + #Inno x64 Installer Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait } else { - Write-Host "x86 Uninstaller unknown, trying the UninstallString from registry..." + Write-Host "x64 Uninstaller unknown, trying the UninstallString from registry..." $NativeUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $UninstallString $Command = $NativeUninstallString.matches.groups[1].value $Parameter = $NativeUninstallString.matches.groups[2].value - #All EXE x86 Installers (native defined uninstall) + #All EXE x64 Installers (native defined uninstall) Start-Process $Command -ArgumentList $Parameter -Wait } } } } + $x64 = $true break } } + if (!$x64) { + $InstalledSoftware = Get-ChildItem "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + foreach ($obj in $InstalledSoftware){ + if ($obj.GetValue('DisplayName') -like $App) { + $UninstallString = $obj.GetValue('UninstallString') + $CleanedUninstallString = $UninstallString.Trim([char]0x0022) + if ($UninstallString -like "MsiExec.exe*") { + $ProductCode = Select-String "{.*}" -inputobject $UninstallString + $ProductCode = $ProductCode.matches.groups[0].value + #MSI x86 Installer + $Exec = Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/x$ProductCode REBOOT=R /qn" -PassThru -Wait + #Stop Hard Reboot (if bad MSI!) + if ($Exec.ExitCode -eq 1641) { + Start-Process "C:\Windows\System32\shutdown.exe" -ArgumentList "/a" + } + } + else { + $QuietUninstallString = $obj.GetValue('QuietUninstallString') + if ($QuietUninstallString) { + $QuietUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $QuietUninstallString + $Command = $QuietUninstallString.matches.groups[1].value + $Parameter = $QuietUninstallString.matches.groups[2].value + #All EXE x86 Installers (already defined silent uninstall) + Start-Process $Command -ArgumentList $Parameter -Wait + } + else { + if ((Test-Path $CleanedUninstallString)) { + $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" + } + if ($NullSoft) { + #NSIS x86 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait + } + else { + if ((Test-Path $CleanedUninstallString)) { + $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" + } + if ($Inno) { + #Inno x86 Installer + Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait + } + else { + Write-Host "x86 Uninstaller unknown, trying the UninstallString from registry..." + $NativeUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $UninstallString + $Command = $NativeUninstallString.matches.groups[1].value + $Parameter = $NativeUninstallString.matches.groups[2].value + #All EXE x86 Installers (native defined uninstall) + Start-Process $Command -ArgumentList $Parameter -Wait + } + } + } + } + break + } + } + } } Return }