2023-09-15 14:33:51 +00:00
# Function to update an App
2023-09-15 14:38:54 +00:00
function Update-App
2023-09-15 14:33:51 +00:00
{
# Get App Info
[ CmdletBinding ( ) ]
param
(
$app
)
$ReleaseNoteURL = Get-AppInfo $app . Id
2023-09-15 14:38:54 +00:00
if ( $ReleaseNoteURL )
2023-09-15 14:33:51 +00:00
{
$Button1Text = $NotifLocale . local . outputs . output [ 10 ] . message
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Send available update notification
Write-ToLog -LogMsg ( 'Updating {0} from {1} to {2}...' -f $app . Name , $app . Version , $app . AvailableVersion ) -LogColor '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 )
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Check if mods exist for preinstall/install/upgrade
$ModsPreInstall , $ModsOverride , $ModsUpgrade , $ModsInstall , $ModsInstalled = Test-Mods $ ( $app . Id )
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Winget upgrade
Write-ToLog -LogMsg ( " ########## WINGET UPGRADE PROCESS STARTS FOR APPLICATION ID '{0}' ########## " -f $app . Id ) -LogColor 'Gray'
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# If PreInstall script exist
2023-09-15 14:38:54 +00:00
if ( $ModsPreInstall )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( 'Modifications for {0} before upgrade are being applied...' -f $app . Id ) -LogColor 'Yellow'
& " $ModsPreInstall "
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Run Winget Upgrade command
2023-09-15 14:38:54 +00:00
if ( $ModsOverride )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( '-> Running (overriding default): Winget upgrade --id {0} --accept-package-agreements --accept-source-agreements --override {1}' -f $app . Id , $ModsOverride )
& $Winget upgrade - -id $ ( $app . Id ) - -accept -package -agreements - -accept -source -agreements - -override $ModsOverride | Tee-Object -FilePath $LogFile -Append
}
2023-09-15 14:38:54 +00:00
else
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( '-> Running: Winget upgrade --id {0} --accept-package-agreements --accept-source-agreements -h' -f $app . Id )
& $Winget upgrade - -id $ ( $app . Id ) - -accept -package -agreements - -accept -source -agreements -h | Tee-Object -FilePath $LogFile -Append
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:38:54 +00:00
if ( $ModsUpgrade )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( 'Modifications for {0} during upgrade are being applied...' -f $app . Id ) -LogColor 'Yellow'
& " $ModsUpgrade "
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Check if application updated properly
$FailedToUpgrade = $false
$ConfirmInstall = Confirm-Installation $ ( $app . Id ) $ ( $app . AvailableVersion )
2023-09-15 14:40:37 +00:00
2023-09-15 14:38:54 +00:00
if ( $ConfirmInstall -ne $true )
2023-09-15 14:33:51 +00:00
{
# Upgrade failed!
# Test for a Pending Reboot (Component Based Servicing/WindowsUpdate/CCM_ClientUtilities)
$PendingReboot = Test-PendingReboot
2023-09-15 14:38:54 +00:00
if ( $PendingReboot -eq $true )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( " -> A Pending Reboot lingers and probably prohibited {0} from upgrading... `n -> ...an install for {1} is NOT executed! " -f $app . Name ) -LogColor 'Red'
$FailedToUpgrade = $true
break
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# If app failed to upgrade, run Install command
Write-ToLog -LogMsg ( '-> An upgrade for {0} failed, now trying an install instead...' -f $app . Name ) -LogColor 'Yellow'
2023-09-15 14:40:37 +00:00
2023-09-15 14:38:54 +00:00
if ( $ModsOverride )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( '-> Running (overriding default): Winget install --id {0} --accept-package-agreements --accept-source-agreements --force --override {1}' -f $app . Id , $ModsOverride )
& $Winget install - -id $ ( $app . Id ) - -accept -package -agreements - -accept -source -agreements - -force - -override $ModsOverride | Tee-Object -FilePath $LogFile -Append
}
2023-09-15 14:38:54 +00:00
else
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( '-> Running: Winget install --id {0} --accept-package-agreements --accept-source-agreements --force -h' -f $app . Id )
& $Winget install - -id $ ( $app . Id ) - -accept -package -agreements - -accept -source -agreements - -force -h | Tee-Object -FilePath $LogFile -Append
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:38:54 +00:00
if ( $ModsInstall )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( 'Modifications for {0} during install are being applied...' -f $app . Id ) -LogColor 'Yellow'
& " $ModsInstall "
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Check if application installed properly
$ConfirmInstall = Confirm-Installation $ ( $app . Id ) $ ( $app . AvailableVersion )
2023-09-15 14:38:54 +00:00
if ( $ConfirmInstall -eq $false )
2023-09-15 14:33:51 +00:00
{
$FailedToUpgrade = $true
}
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:38:54 +00:00
if ( $FailedToUpgrade -eq $false )
2023-09-15 14:33:51 +00:00
{
2023-09-15 14:38:54 +00:00
if ( $ModsInstalled )
2023-09-15 14:33:51 +00:00
{
Write-ToLog -LogMsg ( 'Modifications for {0} after upgrade/install are being applied...' -f $app . Id ) -LogColor 'Yellow'
& " $ModsInstalled "
}
}
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
Write-ToLog -LogMsg ( " ########## WINGET UPGRADE PROCESS FINISHED FOR APPLICATION ID '{0}' ########## " -f $app . Id ) -LogColor 'Gray'
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +00:00
# Notify installation
2023-09-15 14:38:54 +00:00
if ( $FailedToUpgrade -eq $false )
2023-09-15 14:33:51 +00:00
{
# Send success updated app notification
Write-ToLog -LogMsg ( '{0} updated to {1} !' -f $app . Name , $app . AvailableVersion ) -LogColor 'Green'
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +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 )
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
$Script:InstallOK + = 1
}
2023-09-15 14:38:54 +00:00
else
2023-09-15 14:33:51 +00:00
{
# Send failed updated app notification
Write-ToLog -LogMsg ( '{0} update failed.' -f $app . Name ) -LogColor 'Red'
2023-09-15 14:40:37 +00:00
2023-09-15 14:33:51 +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 )
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise -Button1Action $ReleaseNoteURL -Button1Text $Button1Text
}
}