diff --git a/Winget-AutoUpdate/User-Run.ps1 b/Winget-AutoUpdate/User-Run.ps1 index 0c79cfe..8928583 100644 --- a/Winget-AutoUpdate/User-Run.ps1 +++ b/Winget-AutoUpdate/User-Run.ps1 @@ -83,7 +83,7 @@ else { } #Test if there was a list_/winget_error - if (Test-Path "$WorkingDir\logs\*_error.txt") { + if (Test-Path "$WorkingDir\logs\error.txt") { $MessageType = "error" } else { diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index e92c77c..be9b4c1 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -72,8 +72,8 @@ if (Test-Network) { } #Delete previous list_/winget_error (if they exist) if System - if (Test-Path "$WorkingDir\logs\*_error.txt") { - Remove-Item "$WorkingDir\logs\*_error.txt" -Force + if (Test-Path "$WorkingDir\logs\error.txt") { + Remove-Item "$WorkingDir\logs\error.txt" -Force } #Get External ListPath if System @@ -89,7 +89,7 @@ if (Test-Network) { } else { Write-Log "Critical: List doesn't exist, exiting..." "Red" - New-Item "$WorkingDir\logs\list_error.txt" -Force + New-Item "$WorkingDir\logs\error.txt" -Value "List doesn't exist!" -Force Exit 1 } } @@ -135,7 +135,7 @@ if (Test-Network) { if ($outdated -like "Problem:*") { Write-Log "Critical: An error occured, exiting..." "red" Write-Log "$outdated" "red" - New-Item "$WorkingDir\logs\winget_error.txt" -Value "$outdated" -Force + New-Item "$WorkingDir\logs\error.txt" -Value "$outdated" -Force Exit 1 } @@ -216,6 +216,11 @@ if (Test-Network) { Write-Log "User context execution not installed" } } + else { + Write-Log "Critical: An error occured, exiting..." "red" + New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected!" -Force + Exit 1 + } } #End diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 9d09782..8c79824 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -6,7 +6,7 @@ function Invoke-PostUpdateActions { Write-Log "Running Post Update actions..." "yellow" #Reset Winget Sources - $ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') } + $ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') } if ($ResolveWingetPath) { #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path diff --git a/Winget-AutoUpdate/mods/README.md b/Winget-AutoUpdate/mods/README.md index 4242b09..dfecef7 100644 --- a/Winget-AutoUpdate/mods/README.md +++ b/Winget-AutoUpdate/mods/README.md @@ -1 +1,7 @@ -Pre/During/Post install custom scripts should be placed here +Pre/During/Post install/uninstall custom scripts should be placed here. +A script Template and Mods Functions are included as example to get you started... + +Scripts that are considered: +**AppID**`-preinstall.ps1`, `-upgrade.ps1`, `-install.ps1`, `-installed.ps1`, `-preuninstall.ps1`, `-uninstall.ps1` or `-uninstalled.ps1` + +**AppID**`-override.txt` (the content) will be used as a native **winget --override** parameter when upgrading diff --git a/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Winget-AutoUpdate/mods/_AppID-template.ps1 new file mode 100644 index 0000000..db1b47d --- /dev/null +++ b/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -0,0 +1,32 @@ +<# 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 Silently Uninstall (MSI/NSIS/INNO/EXE with defined silent uninstall in registry) +#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 = @("") + +<# FUNCTIONS #> +. $PSScriptRoot\_Mods-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/_Mods-Functions.ps1 b/Winget-AutoUpdate/mods/_Mods-Functions.ps1 new file mode 100644 index 0000000..4c97cf7 --- /dev/null +++ b/Winget-AutoUpdate/mods/_Mods-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" + 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" + 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 +}