Cleanning

pull/29/head
Romain 2022-03-22 14:39:01 +01:00
parent a57f7d838a
commit 94ca44bc49
9 changed files with 63 additions and 65 deletions

View File

@ -1,6 +1,6 @@
#Get locale file for Notification. #Get locale file for Notification.
Function Get-NotifLocal { Function Get-NotifLocale {
#Get OS locale #Get OS locale
$OSLocale = (Get-Culture).Parent $OSLocale = (Get-Culture).Parent

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}
}

View File

@ -1,4 +1,4 @@
function Get-WingetOutdated { function Get-WingetOutdatedApps {
class Software { class Software {
[string]$Name [string]$Name
[string]$Id [string]$Id

View File

@ -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
}
}
}

View File

@ -1,20 +1,20 @@
function Test-Network { function Test-Network {
#init #Init
$timeout = 0 $timeout = 0
#test connectivity during 30 min then timeout #Test connectivity during 30 min then timeout
Write-Log "Checking internet connection..." "Yellow" Write-Log "Checking internet connection..." "Yellow"
While ($timeout -lt 1800){ While ($timeout -lt 1800){
try{ $TestNetwork = Test-NetConnection 8.8.8.8 -Port 443 -InformationLevel Quiet
Invoke-RestMethod -Uri "https://api.github.com/zen" if ($TestNetwork){
Write-Log "Connected !" "Green" Write-Log "Connected !" "Green"
return $true return $true
} }
catch{ else{
Start-Sleep 10 Start-Sleep 10
$timeout += 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){ if ($timeout -eq 300){
Write-Log "Notify 'No connection' sent." "Yellow" Write-Log "Notify 'No connection' sent." "Yellow"
$Title = $NotifLocale.local.outputs.output[0].title $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" Write-Log "Timeout. No internet connection !" "Red"
#Send Notif if no connection for 30 min
$Title = $NotifLocale.local.outputs.output[1].title $Title = $NotifLocale.local.outputs.output[1].title
$Message = $NotifLocale.local.outputs.output[1].message $Message = $NotifLocale.local.outputs.output[1].message
$MessageType = "error" $MessageType = "error"

View File

@ -1,9 +1,5 @@
function Update-WAU{ function Update-WAU ($VersionToUpdate){
#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
#Send available update notification #Send available update notification
$Title = $NotifLocale.local.outputs.output[2].title -f "Winget-AutoUpdate" $Title = $NotifLocale.local.outputs.output[2].title -f "Winget-AutoUpdate"
$Message = $NotifLocale.local.outputs.output[2].message -f $CurrentVersion, $LatestVersion.Replace("v","") $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 New-Item $ZipFile -ItemType File -Force | Out-Null
#Download the zip #Download the zip
Write-Log "Starting downloading the GitHub Repository" Write-Log "Starting downloading the GitHub Repository version $VersionToUpdate"
Invoke-RestMethod -Uri "https://api.github.com/repos/Romanitho/Winget-AutoUpdate/zipball/$($LatestVersion)" -OutFile $ZipFile Invoke-RestMethod -Uri "https://github.com/Romanitho/Winget-AutoUpdate/archive/refs/tags/v$($VersionToUpdate).zip/" -OutFile $ZipFile
Write-Log 'Download finished' Write-Log "Download finished" "Green"
#Extract Zip File #Extract Zip File
Write-Log "Starting unzipping the WAU GitHub Repository" Write-Log "Starting unzipping the WAU GitHub Repository"
$location = "$WorkingDir\WAU_update" $location = "$WorkingDir\WAU_update"
Expand-Archive -Path $ZipFile -DestinationPath $location -Force Expand-Archive -Path $ZipFile -DestinationPath $location -Force
Get-ChildItem -Path $location -Recurse | Unblock-File 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 $TempPath = (Resolve-Path "$location\Romanitho-Winget-AutoUpdate*\Winget-AutoUpdate\").Path
Copy-Item -Path "$TempPath\*" -Destination "$WorkingDir\" -Exclude "icons" -Recurse -Force Copy-Item -Path "$TempPath\*" -Destination "$WorkingDir\" -Exclude "icons" -Recurse -Force
@ -37,9 +33,9 @@ function Update-WAU{
#Remove update folder #Remove update folder
Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue 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 [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") $XMLconf.Save("$WorkingDir\config\about.xml")
#Send success Notif #Send success Notif
@ -50,8 +46,8 @@ function Update-WAU{
Start-NotifTask $Title $Message $MessageType $Balise Start-NotifTask $Title $Message $MessageType $Balise
#Rerun with newer version #Rerun with newer version
Write-Log "Re-run WAU" Write-Log "Re-run WAU"
Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -Command `"$WorkingDir\winget-upgrade`"" Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"$WorkingDir\winget-upgrade`""
exit exit
} }
catch{ catch{

View File

@ -5,21 +5,30 @@ $Script:WorkingDir = $PSScriptRoot
#Get Functions #Get Functions
Get-ChildItem "$WorkingDir\functions" | ForEach-Object {. $_.FullName} Get-ChildItem "$WorkingDir\functions" | ForEach-Object {. $_.FullName}
<# MAIN #> <# MAIN #>
#Run log initialisation function #Run log initialisation function
Start-Init Start-Init
#Get Notif Locale function #Get Notif Locale function
Get-NotifLocal Get-NotifLocale
#Check network connectivity #Check network connectivity
if (Test-Network){ if (Test-Network){
#Check if WAU is up to date #Check if WAU update feature is enabled
$CheckWAUupdate = Start-WAUUpdateCheck $WAUAutoUpdateEnabled = Get-WAUUpdateStatus
#If AutoUpdate is enabled and Update is avalaible, then run WAU update #If yes then check WAU update
if ($CheckWAUupdate){ if ($WAUAutoUpdateEnabled){
Update-WAU #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 #Get exclude apps list
@ -27,7 +36,7 @@ if (Test-Network){
#Get outdated Winget packages #Get outdated Winget packages
Write-Log "Checking available updates..." "yellow" Write-Log "Checking available updates..." "yellow"
$outdated = Get-WingetOutdated $outdated = Get-WingetOutdatedApps
#Log list of app to update #Log list of app to update
foreach ($app in $outdated){ foreach ($app in $outdated){
@ -37,7 +46,7 @@ if (Test-Network){
$Log | out-file -filepath $LogFile -Append $Log | out-file -filepath $LogFile -Append
} }
#Count good update installs #Count good update installations
$InstallOK = 0 $InstallOK = 0
#For each app, notify and update #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 & $UpgradeCmd upgrade --id $($app.Id) --all --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append
#Check if application updated properly #Check if application updated properly
$CheckOutdated = Get-WingetOutdated $CheckOutdated = Get-WingetOutdatedApps
$FailedToUpgrade = $false $FailedToUpgrade = $false
foreach ($CheckApp in $CheckOutdated){ foreach ($CheckApp in $CheckOutdated){
if ($($CheckApp.Id) -eq $($app.Id)) { if ($($CheckApp.Id) -eq $($app.Id)) {
#If app failed to upgrade, run Install command #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 & $upgradecmd install --id $($app.Id) --accept-package-agreements --accept-source-agreements -h | Tee-Object -file $LogFile -Append
#Check if application installed properly #Check if application installed properly
$CheckOutdated2 = Get-WingetOutdated $CheckOutdated2 = Get-WingetOutdatedApps
foreach ($CheckApp2 in $CheckOutdated2){ foreach ($CheckApp2 in $CheckOutdated2){
if ($($CheckApp2.Id) -eq $($app.Id)) { if ($($CheckApp2.Id) -eq $($app.Id)) {
$FailedToUpgrade = $true $FailedToUpgrade = $true