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

127 lines
5.4 KiB
PowerShell
Raw Permalink Normal View History

2023-03-31 15:56:07 +00:00
#Function to update an App
2022-04-13 16:50:06 +00:00
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
2023-03-31 15:56:07 +00:00
if ($ReleaseNoteURL) {
2022-10-26 22:49:10 +00:00
$Button1Text = $NotifLocale.local.outputs.output[10].message
}
2022-04-05 13:17:18 +00:00
#Send available update notification
Write-ToLog "Updating $($app.Name) from $($app.Version) to $($app.AvailableVersion)..." "Cyan"
2022-04-05 13:17:18 +00:00
$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-08 16:53:37 +00:00
$ModsPreInstall, $ModsOverride, $ModsUpgrade, $ModsInstall, $ModsInstalled = Test-Mods $($app.Id)
#Winget upgrade
Write-ToLog "########## WINGET UPGRADE PROCESS STARTS FOR APPLICATION ID '$($App.Id)' ##########" "Gray"
2022-11-26 16:47:38 +00:00
#If PreInstall script exist
if ($ModsPreInstall) {
Write-ToLog "Modifications for $($app.Id) before upgrade are being applied..." "Yellow"
2022-11-26 16:47:38 +00:00
& "$ModsPreInstall"
}
2022-04-13 16:50:06 +00:00
#Run Winget Upgrade command
2022-12-08 16:53:37 +00:00
if ($ModsOverride) {
Write-ToLog "-> Running (overriding default): Winget upgrade --id $($app.Id) --accept-package-agreements --accept-source-agreements --override $ModsOverride"
2022-12-08 16:53:37 +00:00
& $Winget upgrade --id $($app.Id) --accept-package-agreements --accept-source-agreements --override $ModsOverride | Tee-Object -file $LogFile -Append
}
else {
Write-ToLog "-> Running: Winget upgrade --id $($app.Id) --accept-package-agreements --accept-source-agreements -h"
2022-12-08 16:53:37 +00:00
& $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-ToLog "Modifications for $($app.Id) during upgrade are being applied..." "Yellow"
2022-12-04 00:55:40 +00:00
& "$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) {
Write-ToLog "-> 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
Write-ToLog "-> An upgrade for $($app.Name) failed, now trying an install instead..." "Yellow"
2023-03-31 15:56:07 +00:00
2022-12-08 16:53:37 +00:00
if ($ModsOverride) {
Write-ToLog "-> Running (overriding default): Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --override $ModsOverride"
2022-12-08 16:53:37 +00:00
& $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --override $ModsOverride | Tee-Object -file $LogFile -Append
}
else {
Write-ToLog "-> Running: Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h"
2022-12-08 16:53:37 +00:00
& $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-ToLog "Modifications for $($app.Id) during install are being applied..." "Yellow"
2022-12-04 00:55:40 +00:00
& "$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-ToLog "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
}
Write-ToLog "########## 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-ToLog "$($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-ToLog "$($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
}