diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 7ee1479..72bb0db 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -95,13 +95,14 @@ function Install-WingetAutoUpdate{ New-Item -ItemType Directory -Force -Path $WingetUpdatePath } Copy-Item -Path "$PSScriptRoot\Winget-AutoUpdate\*" -Destination $WingetUpdatePath -Recurse -Force -ErrorAction SilentlyContinue + #White List or Black List apps if ($UseWhiteList){ if (Test-Path "$PSScriptRoot\included_apps.txt"){ Copy-Item -Path "$PSScriptRoot\included_apps.txt" -Destination $WingetUpdatePath -Recurse -Force -ErrorAction SilentlyContinue } else{ - New-Item -Path $WingetUpdatePath -Name "included_apps.txt" -ItemType "file" + New-Item -Path $WingetUpdatePath -Name "included_apps.txt" -ItemType "file" -ErrorAction SilentlyContinue } } else { diff --git a/Winget-AutoUpdate/config/about.xml b/Winget-AutoUpdate/config/about.xml index 09ebf9e..c5291b8 100644 --- a/Winget-AutoUpdate/config/about.xml +++ b/Winget-AutoUpdate/config/about.xml @@ -1,7 +1,7 @@ Winget-AutoUpdate (WAU) - 1.6.3 + 1.7.0 Romanitho https://github.com/Romanitho/Winget-AutoUpdate diff --git a/Winget-AutoUpdate/functions/Get-IncludedApps.ps1 b/Winget-AutoUpdate/functions/Get-IncludedApps.ps1 new file mode 100644 index 0000000..aee2a87 --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-IncludedApps.ps1 @@ -0,0 +1,5 @@ +function Get-IncludedApps{ + if (Test-Path "$WorkingDir\included_apps.txt"){ + return Get-Content -Path "$WorkingDir\included_apps.txt" + } +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Get-WAUConfig.ps1 b/Winget-AutoUpdate/functions/Get-WAUConfig.ps1 new file mode 100644 index 0000000..4d89ea4 --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-WAUConfig.ps1 @@ -0,0 +1,14 @@ +function Get-WAUConfig{ + + [xml]$WAUConfig = Get-Content "$WorkingDir\config\config.xml" -Encoding UTF8 -ErrorAction SilentlyContinue + + #Check if WAU is configured for Black or White List + if ($true -eq [System.Convert]::ToBoolean($WAUConfig.app.UseWAUWhiteList)){ + Write-Log "WAU uses White List config" + $Script:UseWhiteList = $true + } + else{ + Write-Log "WAU uses Black List config" + $Script:UseWhiteList = $false + } +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 b/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 index ffe576a..2817235 100644 --- a/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 +++ b/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 @@ -10,15 +10,15 @@ function Get-WingetOutdatedApps { $WingetPath = (Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe").Path #Get Winget Location in User context if ($WingetCmd){ - $Script:winget = (Get-Command winget.exe -ErrorAction SilentlyContinue).Source + $Script:Winget = (Get-Command winget.exe -ErrorAction SilentlyContinue).Source } #Get Winget Location in System context (WinGet < 1.17) elseif (Test-Path "$WingetPath\AppInstallerCLI.exe"){ - $Script:winget = "$WingetPath\AppInstallerCLI.exe" + $Script:Winget = "$WingetPath\AppInstallerCLI.exe" } #Get Winget Location in System context (WinGet > 1.17) elseif (Test-Path "$WingetPath\winget.exe"){ - $Script:winget = "$WingetPath\winget.exe" + $Script:Winget = "$WingetPath\winget.exe" } else{ Write-Log "Winget not installed !" "Red" @@ -26,10 +26,10 @@ function Get-WingetOutdatedApps { } #Run winget to list apps and accept source agrements (necessary on first run) - & $upgradecmd list --accept-source-agreements | Out-Null + & $Winget list --accept-source-agreements | Out-Null #Get list of available upgrades on winget format - $upgradeResult = & $upgradecmd upgrade | Out-String + $upgradeResult = & $Winget upgrade | Out-String #Start Convertion of winget format to an array. Check if "-----" exists if (!($upgradeResult -match "-----")){ diff --git a/Winget-AutoUpdate/functions/Update-App.ps1 b/Winget-AutoUpdate/functions/Update-App.ps1 new file mode 100644 index 0000000..28545ce --- /dev/null +++ b/Winget-AutoUpdate/functions/Update-App.ps1 @@ -0,0 +1,59 @@ +Function Update-App ($app) { + + #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) + Start-NotifTask $Title $Message $MessageType $Balise + + #Winget upgrade + Write-Log "########## WINGET UPGRADE PROCESS STARTS FOR APPLICATION ID '$($App.Id)' ##########" "Gray" + #Run Winget Upgrade command + & $Winget upgrade --id $($app.Id) --all --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append + + #Check if application updated properly + $CheckOutdated = Get-WingetOutdatedApps + $FailedToUpgrade = $false + foreach ($CheckApp in $CheckOutdated){ + if ($($CheckApp.Id) -eq $($app.Id)) { + #If app failed to upgrade, run Install command + & $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append + #Check if application installed properly + $CheckOutdated2 = Get-WingetOutdatedApps + foreach ($CheckApp2 in $CheckOutdated2){ + if ($($CheckApp2.Id) -eq $($app.Id)) { + $FailedToUpgrade = $true + } + } + } + } + Write-Log "########## WINGET UPGRADE PROCESS FINISHED FOR APPLICATION ID '$($App.Id)' ##########" "Gray" + + #Notify installation + if ($FailedToUpgrade -eq $false){ + #Send success updated app notification + Write-Log "$($app.Name) updated to $($app.AvailableVersion) !" "Green" + + #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 $Message $MessageType $Balise + + $InstallOK += 1 + } + else { + #Send failed updated app notification + Write-Log "$($app.Name) update failed." "Red" + + #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 $Message $MessageType $Balise + } +} \ No newline at end of file diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1 index f526a31..692cafb 100644 --- a/Winget-AutoUpdate/winget-upgrade.ps1 +++ b/Winget-AutoUpdate/winget-upgrade.ps1 @@ -35,8 +35,14 @@ if (Test-Network){ } } - #Get exclude apps list - $toSkip = Get-ExcludedApps + #Get White or Black list + Get-WAUConfig + if ($UseWhiteList){ + $toUpdate = Get-IncludedApps + } + else{ + $toSkip = Get-ExcludedApps + } #Get outdated Winget packages Write-Log "Checking application updates on Winget Repository..." "yellow" @@ -51,80 +57,43 @@ if (Test-Network){ } #Count good update installations - $InstallOK = 0 + $Script:InstallOK = 0 - #For each app, notify and update - foreach ($app in $outdated){ - - if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown"){ - - #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) - Start-NotifTask $Title $Message $MessageType $Balise - - #Winget upgrade - Write-Log "########## WINGET UPGRADE PROCESS STARTS FOR APPLICATION ID '$($App.Id)' ##########" "Gray" - #Run Winget Upgrade command - & $UpgradeCmd upgrade --id $($app.Id) --all --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append - - #Check if application updated properly - $CheckOutdated = Get-WingetOutdatedApps - $FailedToUpgrade = $false - foreach ($CheckApp in $CheckOutdated){ - if ($($CheckApp.Id) -eq $($app.Id)) { - #If app failed to upgrade, run Install command - & $upgradecmd install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append - #Check if application installed properly - $CheckOutdated2 = Get-WingetOutdatedApps - foreach ($CheckApp2 in $CheckOutdated2){ - if ($($CheckApp2.Id) -eq $($app.Id)) { - $FailedToUpgrade = $true - } - } - } - } - Write-Log "########## WINGET UPGRADE PROCESS FINISHED FOR APPLICATION ID '$($App.Id)' ##########" "Gray" - - #Notify installation - if ($FailedToUpgrade -eq $false){ - #Send success updated app notification - Write-Log "$($app.Name) updated to $($app.AvailableVersion) !" "Green" - - #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 $Message $MessageType $Balise - - $InstallOK += 1 + #If White List + if ($UseWhiteList){ + #For each app, notify and update + foreach ($app in $outdated){ + if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown"){ + Update-App $app } - else { - #Send failed updated app notification - Write-Log "$($app.Name) update failed." "Red" - - #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 $Message $MessageType $Balise + #if current app version is unknown + elseif($($app.Version) -eq "Unknown"){ + Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray" + } + #if app is in "excluded list" + else{ + Write-Log "$($app.Name) : Skipped upgrade because it is not in the included app list" "Gray" } - } - #if current app version is unknown - elseif($($app.Version) -eq "Unknown"){ - Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray" - } - #if app is in "excluded list" - else{ - Write-Log "$($app.Name) : Skipped upgrade because it is in the excluded app list" "Gray" } } - + #If Black List + else{ + #For each app, notify and update + foreach ($app in $outdated){ + if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown"){ + Update-App $app + } + #if current app version is unknown + elseif($($app.Version) -eq "Unknown"){ + Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray" + } + #if app is in "excluded list" + else{ + Write-Log "$($app.Name) : Skipped upgrade because it is in the excluded app list" "Gray" + } + } + } + if ($InstallOK -gt 0){ Write-Log "$InstallOK apps updated ! No more update." "Green" } diff --git a/install.bat b/install.bat index 6b58eab..3bb1be3 100644 --- a/install.bat +++ b/install.bat @@ -1,2 +1,2 @@ @echo off -powershell -Command "Get-ChildItem -Path '%~dp0' -Recurse | Unblock-File; Start-Process powershell.exe -Argument '-executionpolicy bypass -file """%~dp0Winget-AutoUpdate-Install.ps1"" -Silent'" -Verb RunAs +powershell -Command "Get-ChildItem -Path '%~dp0' -Recurse | Unblock-File; Start-Process powershell.exe -Argument '-executionpolicy bypass -file """%~dp0Winget-AutoUpdate-Install.ps1"" -UseWhiteList -Silent'" -Verb RunAs