diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index 45288488..29a00e81 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -1,13 +1,21 @@ #Common shared functions to handle the mods -function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { - if (Test-Path "$Run") { - if (!$RunSwitch) { $RunSwitch = " " } - if (!$User) { - if (!$RunWait) { +function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) +{ + if (Test-Path "$Run") + { + if (!$RunSwitch) + { + $RunSwitch = " " + } + if (!$User) + { + if (!$RunWait) + { Start-Process $Run -ArgumentList $RunSwitch } - else { + else + { Start-Process $Run -ArgumentList $RunSwitch -Wait } } @@ -19,8 +27,10 @@ function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { } -function Stop-ModsProc ($Proc) { - foreach ($process in $Proc) { +function Stop-ModsProc ($Proc) +{ + foreach ($process in $Proc) + { Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null } Return @@ -32,147 +42,140 @@ function Stop-ModsSvc ($Svc) { Return } -function Wait-ModsProc ($Wait) { - foreach ($process in $Wait) { +function Wait-ModsProc ($Wait) +{ + foreach ($process in $Wait) + { Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } } Return } -function Install-WingetID ($WingetIDInst) { - foreach ($app in $WingetIDInst) { +function Install-WingetID ($WingetIDInst) +{ + foreach ($app in $WingetIDInst) + { & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h } Return } -function Uninstall-WingetID ($WingetIDUninst) { - foreach ($app in $WingetIDUninst) { +function Uninstall-WingetID ($WingetIDUninst) +{ + foreach ($app in $WingetIDUninst) + { & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h } Return } -function Uninstall-ModsApp ($AppUninst, $AllVersions) { - foreach ($app in $AppUninst) { - $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" - } +function Uninstall-ModsApp ($AppUninst, $AllVersions) +{ + foreach ($app in $AppUninst) + { + # we start from scanning the x64 node in registry, if something was found, then we set x64=TRUE + [bool]$app_was_x64 = Process-installedSoftware -app $app -x64 $true; + + # if nothing was found in x64 node, then we repeat that action in x86 node + if (!$app_was_x64) + { + Process-installedSoftware -app $app | Out-Null; + } + } + Return +} + +Function Process-installedSoftware() +{ + [OutputType([Bool])] + Param( + [parameter(Mandatory=$true)] [string]$app, + [parameter(Mandatory=$false)][bool] $x64 = $false + ) + if($true -eq $x64) + { + [string]$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"; + } + else + { + [string]$path = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; + } + + [bool]$app_was_found = $false; + [Microsoft.Win32.RegistryKey[]]$InstalledSoftware = Get-ChildItem $path; + 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 { - $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" } - 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 - } - } + if ($NullSoft) + { + #NSIS x64 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait } - } - $x64 = $true - if (!$AllVersions) { - 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 + { + if ((Test-Path $CleanedUninstallString)) + { + $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" } - } - 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 + if ($Inno) + { + #Inno x64 Installer + Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -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 - } - } + else + { + Write-Host "$(if($true -eq $x64) {'x64'} else {'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 x64 Installers (native defined uninstall) + Start-Process $Command -ArgumentList $Parameter -Wait } } - if (!$AllVersions) { - break - } } } + $app_was_found = $true + if (!$AllVersions) + { + break + } } } - Return + return $app_was_found; } + function Remove-ModsLnk ($Lnk) { foreach ($link in $Lnk) { Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null