wingetautoupdate/Winget-AutoUpdate/functions/Update-App.ps1

114 lines
4.6 KiB
PowerShell
Raw Normal View History

2022-04-13 16:50:06 +00:00
#Function to Update an App
2022-04-05 13:17:18 +00:00
Function Update-App ($app) {
#Get App Info
2022-10-26 22:49:10 +00:00
$ReleaseNoteURL = Get-AppInfo $app.Id
if ($ReleaseNoteURL){
$Button1Text = $NotifLocale.local.outputs.output[10].message
}
2022-04-05 13:17:18 +00:00
#Send available update notification
Write-Log "Updating $($app.Name) from $($app.Version) to $($app.AvailableVersion)..." "Cyan"
$Title = $NotifLocale.local.outputs.output[2].title -f $($app.Name)
$Message = $NotifLocale.local.outputs.output[2].message -f $($app.Version), $($app.AvailableVersion)
$MessageType = "info"
$Balise = $($app.Name)
2022-10-26 22:49:10 +00:00
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
2022-04-05 13:17:18 +00:00
2022-11-26 16:47:38 +00:00
#Check if mods exist for preinstall/install/upgrade
2022-12-04 00:55:40 +00:00
$ModsPreInstall, $ModsUpgrade, $ModsInstall, $ModsInstalled = Test-Mods $($app.Id)
2022-11-26 16:47:38 +00:00
#If PreInstall script exist
if ($ModsPreInstall) {
Write-Log "Modifications for $($app.Id) before upgrade are being applied..." "Yellow"
& "$ModsPreInstall"
}
2022-04-05 13:17:18 +00:00
#Winget upgrade
Write-Log "########## WINGET UPGRADE PROCESS STARTS FOR APPLICATION ID '$($App.Id)' ##########" "Gray"
2022-10-26 22:49:10 +00:00
2022-04-13 16:50:06 +00:00
#Run Winget Upgrade command
2022-08-10 08:16:29 +00:00
Write-Log "-> Running: Winget upgrade --id $($app.Id) --accept-package-agreements --accept-source-agreements -h"
& $Winget upgrade --id $($app.Id) --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append
2022-07-27 07:44:11 +00:00
2022-12-04 00:55:40 +00:00
if ($ModsUpgrade) {
Write-Log "Modifications for $($app.Id) during upgrade are being applied..." "Yellow"
& "$ModsUpgrade"
}
2022-07-28 17:09:50 +00:00
2022-04-13 16:50:06 +00:00
#Check if application updated properly
$CheckOutdated = Get-WingetOutdatedApps
$FailedToUpgrade = $false
2022-06-10 08:26:41 +00:00
foreach ($CheckApp in $CheckOutdated) {
2022-04-13 16:50:06 +00:00
if ($($CheckApp.Id) -eq $($app.Id)) {
2022-10-26 22:49:10 +00:00
#Upgrade failed!
#Test for a Pending Reboot (Component Based Servicing/WindowsUpdate/CCM_ClientUtilities)
$PendingReboot = Test-PendingReboot
if ($PendingReboot -eq $true) {
2022-08-10 08:16:29 +00:00
Write-Log "-> A Pending Reboot lingers and probably prohibited $($app.Name) from upgrading...`n...an install for $($app.Name) is NOT executed!" "Red"
$FailedToUpgrade = $true
break
}
2022-10-26 22:49:10 +00:00
#If app failed to upgrade, run Install command
2022-08-10 08:16:29 +00:00
Write-Log "-> An upgrade for $($app.Name) failed, now trying an install instead..." "Yellow"
Write-Log "-> Running: Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h"
& $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append
2022-12-04 00:55:40 +00:00
if ($ModsInstall) {
Write-Log "Modifications for $($app.Id) during install are being applied..." "Yellow"
& "$ModsInstall"
}
#Check if application installed properly
$CheckOutdated2 = Get-WingetOutdatedApps
foreach ($CheckApp2 in $CheckOutdated2) {
if ($($CheckApp2.Id) -eq $($app.Id)) {
$FailedToUpgrade = $true
}
}
2022-04-05 13:17:18 +00:00
}
2022-04-13 16:50:06 +00:00
}
2022-07-27 15:40:40 +00:00
if ($FailedToUpgrade -eq $false) {
2022-12-04 00:55:40 +00:00
if ($ModsInstalled) {
Write-Log "Modifications for $($app.Id) after upgrade/install are being applied..." "Yellow"
2022-12-04 01:30:28 +00:00
& "$ModsInstalled"
2022-07-28 17:09:50 +00:00
}
2022-07-27 15:40:40 +00:00
}
2022-10-26 22:49:10 +00:00
Write-Log "########## WINGET UPGRADE PROCESS FINISHED FOR APPLICATION ID '$($App.Id)' ##########" "Gray"
2022-04-05 13:17:18 +00:00
#Notify installation
2022-10-26 22:49:10 +00:00
if ($FailedToUpgrade -eq $false) {
2022-04-13 16:50:06 +00:00
2022-04-05 13:17:18 +00:00
#Send success updated app notification
Write-Log "$($app.Name) updated to $($app.AvailableVersion) !" "Green"
2022-10-26 22:49:10 +00:00
2022-04-05 13:17:18 +00:00
#Send Notif
$Title = $NotifLocale.local.outputs.output[3].title -f $($app.Name)
$Message = $NotifLocale.local.outputs.output[3].message -f $($app.AvailableVersion)
$MessageType = "success"
$Balise = $($app.Name)
2022-10-26 22:49:10 +00:00
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
2022-04-05 13:17:18 +00:00
2022-04-23 18:09:55 +00:00
$Script:InstallOK += 1
2022-10-26 22:49:10 +00:00
2022-04-05 13:17:18 +00:00
}
else {
2022-04-13 16:50:06 +00:00
2022-04-05 13:17:18 +00:00
#Send failed updated app notification
Write-Log "$($app.Name) update failed." "Red"
2022-10-26 22:49:10 +00:00
2022-04-05 13:17:18 +00:00
#Send Notif
$Title = $NotifLocale.local.outputs.output[4].title -f $($app.Name)
$Message = $NotifLocale.local.outputs.output[4].message
$MessageType = "error"
$Balise = $($app.Name)
2022-10-26 22:49:10 +00:00
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
2022-04-05 13:17:18 +00:00
}
2022-04-13 16:50:06 +00:00
2022-04-05 13:17:18 +00:00
}