From 9d9945d05774a1c7468b6cad954320cbca2f0a10 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Tue, 20 Dec 2022 02:54:14 +0100 Subject: [PATCH] Template and Functions done --- Winget-AutoUpdate/mods/_AppID-template.ps1 | 31 +++++ Winget-AutoUpdate/mods/_Common-Functions.ps1 | 117 +++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 Winget-AutoUpdate/mods/_AppID-template.ps1 create mode 100644 Winget-AutoUpdate/mods/_Common-Functions.ps1 diff --git a/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Winget-AutoUpdate/mods/_AppID-template.ps1 new file mode 100644 index 0000000..342397a --- /dev/null +++ b/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -0,0 +1,31 @@ +<# ARRAYS/VARIABLES #> +#Beginning of Process Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" +$Proc = @("") + +#Beginning of Process Name to Wait for to end - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" +$Wait = @("") + +#Beginning of App Name string to Uninstall - required wildcard (*) after! +$App = "" + +#Beginning of Desktop Link Name to Remove - optional wildcard (*) after, without .lnk, multiple: "lnk1","lnk2" +$Lnk = @("") + +<# FUNCTIONS #> +. $PSScriptRoot\_Common-Functions.ps1 + +<# MAIN #> +if ($Proc) { + Stop-ModsProc $Proc +} +if ($Wait) { + Wait-ModsProc $Wait +} +if ($App) { + Uninstall-ModsApp $App +} +if ($Lnk) { + Remove-ModsLnk $Lnk +} + +<# EXTRAS #> diff --git a/Winget-AutoUpdate/mods/_Common-Functions.ps1 b/Winget-AutoUpdate/mods/_Common-Functions.ps1 new file mode 100644 index 0000000..541f178 --- /dev/null +++ b/Winget-AutoUpdate/mods/_Common-Functions.ps1 @@ -0,0 +1,117 @@ +#Common shared functions for mods handling + +function Stop-ModsProc ($Proc) { + foreach ($process in $Proc) + { + Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null + } + Return +} +function Wait-ModsProc ($Wait) { + foreach ($process in $Wait) + { + Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } + } + 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') + 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 { + $NullSoft = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Nullsoft.NSIS" + if ($NullSoft) { + #NSIS x64 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait + } + else { + $Inno = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Inno Setup" + if ($Inno) { + #Inno x64 Installer + Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait + } + else { + Write-Host "x64 Uninstaller unknown..." + } + } + } + } + $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') + 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 { + $NullSoft = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Nullsoft.NSIS" + if ($NullSoft) { + #NSIS x86 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait + } + else { + $Inno = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Inno Setup" + if ($Inno) { + #Inno x86 Installer + Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait + } + else { + Write-Host "x86 Uninstaller unknown..." + } + } + } + } + break + } + } + } + Return +} +function Remove-ModsLnk ($Lnk) { + foreach ($link in $Lnk) + { + Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null + } + Return +}