From 94ca44bc495ae8f13c7290a1bcb4929a5c14c530 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Tue, 22 Mar 2022 14:39:01 +0100 Subject: [PATCH] Cleanning --- ...Get-NotifLocal.ps1 => Get-NotifLocale.ps1} | 2 +- .../functions/Get-WAUAvailableVersion.ps1 | 6 ++++ .../functions/Get-WAUCurrentVersion.ps1 | 6 ++++ .../functions/Get-WAUUpdateStatus.ps1 | 12 +++++++ ...utdated.ps1 => Get-WingetOutdatedApps.ps1} | 2 +- .../functions/Start-WAUUpdateCheck.ps1 | 32 ------------------- Winget-AutoUpdate/functions/Test-Network.ps1 | 17 +++++----- Winget-AutoUpdate/functions/Update-WAU.ps1 | 22 ++++++------- Winget-AutoUpdate/winget-upgrade.ps1 | 29 +++++++++++------ 9 files changed, 63 insertions(+), 65 deletions(-) rename Winget-AutoUpdate/functions/{Get-NotifLocal.ps1 => Get-NotifLocale.ps1} (95%) create mode 100644 Winget-AutoUpdate/functions/Get-WAUAvailableVersion.ps1 create mode 100644 Winget-AutoUpdate/functions/Get-WAUCurrentVersion.ps1 create mode 100644 Winget-AutoUpdate/functions/Get-WAUUpdateStatus.ps1 rename Winget-AutoUpdate/functions/{Get-WingetOutdated.ps1 => Get-WingetOutdatedApps.ps1} (98%) delete mode 100644 Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1 diff --git a/Winget-AutoUpdate/functions/Get-NotifLocal.ps1 b/Winget-AutoUpdate/functions/Get-NotifLocale.ps1 similarity index 95% rename from Winget-AutoUpdate/functions/Get-NotifLocal.ps1 rename to Winget-AutoUpdate/functions/Get-NotifLocale.ps1 index 483edab..388f254 100644 --- a/Winget-AutoUpdate/functions/Get-NotifLocal.ps1 +++ b/Winget-AutoUpdate/functions/Get-NotifLocale.ps1 @@ -1,6 +1,6 @@ #Get locale file for Notification. -Function Get-NotifLocal { +Function Get-NotifLocale { #Get OS locale $OSLocale = (Get-Culture).Parent diff --git a/Winget-AutoUpdate/functions/Get-WAUAvailableVersion.ps1 b/Winget-AutoUpdate/functions/Get-WAUAvailableVersion.ps1 new file mode 100644 index 0000000..bfe0af0 --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-WAUAvailableVersion.ps1 @@ -0,0 +1,6 @@ +function Get-WAUAvailableVersion{ + #Get Github latest version + $WAUurl = 'https://api.github.com/repos/Romanitho/Winget-AutoUpdate/releases/latest' + $Script:WAULatestVersion = ((Invoke-WebRequest $WAUurl -UseBasicParsing | ConvertFrom-Json)[0].tag_name).Replace("v","") + return [version]$WAULatestVersion +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Get-WAUCurrentVersion.ps1 b/Winget-AutoUpdate/functions/Get-WAUCurrentVersion.ps1 new file mode 100644 index 0000000..49e3e2d --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-WAUCurrentVersion.ps1 @@ -0,0 +1,6 @@ +function Get-WAUCurrentVersion{ + #Get current installed version + [xml]$About = Get-Content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue + $Script:WAUCurrentVersion = $About.app.version + return [version]$WAUCurrentVersion +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Get-WAUUpdateStatus.ps1 b/Winget-AutoUpdate/functions/Get-WAUUpdateStatus.ps1 new file mode 100644 index 0000000..477ac37 --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-WAUUpdateStatus.ps1 @@ -0,0 +1,12 @@ +function Get-WAUUpdateStatus{ + #Get AutoUpdate status + [xml]$UpdateStatus = Get-Content "$WorkingDir\config\config.xml" -Encoding UTF8 -ErrorAction SilentlyContinue + if ($true -eq $UpdateStatus.app.WAUautoupdate){ + Write-Log "WAU AutoUpdate is Enabled" "Green" + return $true + } + else{ + Write-Log "WAU AutoUpdate is Disabled" "Grey" + return $false + } +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Get-WingetOutdated.ps1 b/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 similarity index 98% rename from Winget-AutoUpdate/functions/Get-WingetOutdated.ps1 rename to Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 index 11ff4bf..b6ab413 100644 --- a/Winget-AutoUpdate/functions/Get-WingetOutdated.ps1 +++ b/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1 @@ -1,4 +1,4 @@ -function Get-WingetOutdated { +function Get-WingetOutdatedApps { class Software { [string]$Name [string]$Id diff --git a/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1 b/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1 deleted file mode 100644 index d072075..0000000 --- a/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1 +++ /dev/null @@ -1,32 +0,0 @@ -function Start-WAUUpdateCheck{ - #Get AutoUpdate status - [xml]$UpdateStatus = Get-Content "$WorkingDir\config\config.xml" -Encoding UTF8 -ErrorAction SilentlyContinue - $AutoUpdateStatus = $UpdateStatus.app.WAUautoupdate - - #Get current installed version - [xml]$About = Get-Content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue - [version]$Script:CurrentVersion = $About.app.version - - #Check if AutoUpdate is enabled - if ($AutoUpdateStatus -eq $false){ - Write-Log "WAU Current version: $CurrentVersion. AutoUpdate is disabled." "Cyan" - return $false - } - #If enabled, check online available version - else{ - #Get Github latest version - $WAUurl = 'https://api.github.com/repos/Romanitho/Winget-AutoUpdate/releases/latest' - $LatestVersion = (Invoke-WebRequest $WAUurl -UseBasicParsing | ConvertFrom-Json)[0].tag_name - [version]$AvailableVersion = $LatestVersion.Replace("v","") - - #If newer version is avalable, return $True - if ($AvailableVersion -gt $CurrentVersion){ - Write-Log "WAU Current version: $CurrentVersion. Version $AvailableVersion is available." "Yellow" - return $true - } - else{ - Write-Log "WAU Current version: $CurrentVersion. Up to date." "Green" - return $false - } - } -} diff --git a/Winget-AutoUpdate/functions/Test-Network.ps1 b/Winget-AutoUpdate/functions/Test-Network.ps1 index 422235a..9ce37e8 100644 --- a/Winget-AutoUpdate/functions/Test-Network.ps1 +++ b/Winget-AutoUpdate/functions/Test-Network.ps1 @@ -1,20 +1,20 @@ function Test-Network { - #init + #Init $timeout = 0 - #test connectivity during 30 min then timeout + #Test connectivity during 30 min then timeout Write-Log "Checking internet connection..." "Yellow" While ($timeout -lt 1800){ - try{ - Invoke-RestMethod -Uri "https://api.github.com/zen" + $TestNetwork = Test-NetConnection 8.8.8.8 -Port 443 -InformationLevel Quiet + if ($TestNetwork){ Write-Log "Connected !" "Green" return $true } - catch{ + else{ Start-Sleep 10 $timeout += 10 - Write-Log "Checking internet connection. $($timeout)s." "Yellow" - #Send Notif if no connection for 5 min + + #Send Warning Notif if no connection for 5 min if ($timeout -eq 300){ Write-Log "Notify 'No connection' sent." "Yellow" $Title = $NotifLocale.local.outputs.output[0].title @@ -25,8 +25,9 @@ function Test-Network { } } } + + #Send Timeout Notif if no connection for 30 min Write-Log "Timeout. No internet connection !" "Red" - #Send Notif if no connection for 30 min $Title = $NotifLocale.local.outputs.output[1].title $Message = $NotifLocale.local.outputs.output[1].message $MessageType = "error" diff --git a/Winget-AutoUpdate/functions/Update-WAU.ps1 b/Winget-AutoUpdate/functions/Update-WAU.ps1 index a025cbb..23f2540 100644 --- a/Winget-AutoUpdate/functions/Update-WAU.ps1 +++ b/Winget-AutoUpdate/functions/Update-WAU.ps1 @@ -1,9 +1,5 @@ -function Update-WAU{ - #Get WAU Github latest version - $WAUurl = 'https://api.github.com/repos/Romanitho/Winget-AutoUpdate/releases/latest' - $LatestVersion = (Invoke-WebRequest $WAUurl -UseBasicParsing | ConvertFrom-Json)[0].tag_name - +function Update-WAU ($VersionToUpdate){ #Send available update notification $Title = $NotifLocale.local.outputs.output[2].title -f "Winget-AutoUpdate" $Message = $NotifLocale.local.outputs.output[2].message -f $CurrentVersion, $LatestVersion.Replace("v","") @@ -18,16 +14,16 @@ function Update-WAU{ New-Item $ZipFile -ItemType File -Force | Out-Null #Download the zip - Write-Log "Starting downloading the GitHub Repository" - Invoke-RestMethod -Uri "https://api.github.com/repos/Romanitho/Winget-AutoUpdate/zipball/$($LatestVersion)" -OutFile $ZipFile - Write-Log 'Download finished' + Write-Log "Starting downloading the GitHub Repository version $VersionToUpdate" + Invoke-RestMethod -Uri "https://github.com/Romanitho/Winget-AutoUpdate/archive/refs/tags/v$($VersionToUpdate).zip/" -OutFile $ZipFile + Write-Log "Download finished" "Green" #Extract Zip File Write-Log "Starting unzipping the WAU GitHub Repository" $location = "$WorkingDir\WAU_update" Expand-Archive -Path $ZipFile -DestinationPath $location -Force Get-ChildItem -Path $location -Recurse | Unblock-File - Write-Log "Unzip finished" + Write-Log "Unzip finished" "Green" $TempPath = (Resolve-Path "$location\Romanitho-Winget-AutoUpdate*\Winget-AutoUpdate\").Path Copy-Item -Path "$TempPath\*" -Destination "$WorkingDir\" -Exclude "icons" -Recurse -Force @@ -37,9 +33,9 @@ function Update-WAU{ #Remove update folder Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue - #Set new version to conf.xml + #Set new version to about.xml [xml]$XMLconf = Get-content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue - $XMLconf.app.version = $LatestVersion.Replace("v","") + $XMLconf.app.version = $VersionToUpdate $XMLconf.Save("$WorkingDir\config\about.xml") #Send success Notif @@ -50,8 +46,8 @@ function Update-WAU{ Start-NotifTask $Title $Message $MessageType $Balise #Rerun with newer version - Write-Log "Re-run WAU" - Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -Command `"$WorkingDir\winget-upgrade`"" + Write-Log "Re-run WAU" + Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"$WorkingDir\winget-upgrade`"" exit } catch{ diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1 index 8831239..c890b25 100644 --- a/Winget-AutoUpdate/winget-upgrade.ps1 +++ b/Winget-AutoUpdate/winget-upgrade.ps1 @@ -5,21 +5,30 @@ $Script:WorkingDir = $PSScriptRoot #Get Functions Get-ChildItem "$WorkingDir\functions" | ForEach-Object {. $_.FullName} + <# MAIN #> #Run log initialisation function Start-Init #Get Notif Locale function -Get-NotifLocal +Get-NotifLocale #Check network connectivity if (Test-Network){ - #Check if WAU is up to date - $CheckWAUupdate = Start-WAUUpdateCheck - #If AutoUpdate is enabled and Update is avalaible, then run WAU update - if ($CheckWAUupdate){ - Update-WAU + #Check if WAU update feature is enabled + $WAUAutoUpdateEnabled = Get-WAUUpdateStatus + #If yes then check WAU update + if ($WAUAutoUpdateEnabled){ + #Get Current Version + $WAUCurrentVersion = Get-WAUCurrentVersion + #Get Available Version + $WAUAvailableVersion = Get-WAUAvailableVersion + #Compare + if ($WAUAvailableVersion -gt $WAUCurrentVersion){ + #If new version is available, update it + Update-WAU $WAUAvailableVersion + } } #Get exclude apps list @@ -27,7 +36,7 @@ if (Test-Network){ #Get outdated Winget packages Write-Log "Checking available updates..." "yellow" - $outdated = Get-WingetOutdated + $outdated = Get-WingetOutdatedApps #Log list of app to update foreach ($app in $outdated){ @@ -37,7 +46,7 @@ if (Test-Network){ $Log | out-file -filepath $LogFile -Append } - #Count good update installs + #Count good update installations $InstallOK = 0 #For each app, notify and update @@ -59,14 +68,14 @@ if (Test-Network){ & $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-WingetOutdated + $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-WingetOutdated + $CheckOutdated2 = Get-WingetOutdatedApps foreach ($CheckApp2 in $CheckOutdated2){ if ($($CheckApp2.Id) -eq $($app.Id)) { $FailedToUpgrade = $true