From a4404dfb184ce31622925a09c64b9ff45cfc072f Mon Sep 17 00:00:00 2001
From: Romain <96626929+Romanitho@users.noreply.github.com>
Date: Mon, 14 Mar 2022 14:55:02 +0100
Subject: [PATCH] Start cleaning
---
.../functions/Get-ExcludedApps.ps1 | 5 +
.../functions/Get-NotifLocal.ps1 | 24 ++
.../functions/Get-WingetOutdated.ps1 | 75 ++++
Winget-AutoUpdate/functions/Start-Init.ps1 | 30 ++
.../functions/Start-NotifTask.ps1 | 52 +++
.../functions/Start-WAUUpdateCheck.ps1 | 32 ++
Winget-AutoUpdate/functions/Test-Network.ps1 | 36 ++
Winget-AutoUpdate/functions/Update-WAU.ps1 | 66 ++++
Winget-AutoUpdate/functions/Write-Log.ps1 | 9 +
Winget-AutoUpdate/winget-upgrade.ps1 | 339 +-----------------
10 files changed, 339 insertions(+), 329 deletions(-)
create mode 100644 Winget-AutoUpdate/functions/Get-ExcludedApps.ps1
create mode 100644 Winget-AutoUpdate/functions/Get-NotifLocal.ps1
create mode 100644 Winget-AutoUpdate/functions/Get-WingetOutdated.ps1
create mode 100644 Winget-AutoUpdate/functions/Start-Init.ps1
create mode 100644 Winget-AutoUpdate/functions/Start-NotifTask.ps1
create mode 100644 Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1
create mode 100644 Winget-AutoUpdate/functions/Test-Network.ps1
create mode 100644 Winget-AutoUpdate/functions/Update-WAU.ps1
create mode 100644 Winget-AutoUpdate/functions/Write-Log.ps1
diff --git a/Winget-AutoUpdate/functions/Get-ExcludedApps.ps1 b/Winget-AutoUpdate/functions/Get-ExcludedApps.ps1
new file mode 100644
index 0000000..efe9409
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Get-ExcludedApps.ps1
@@ -0,0 +1,5 @@
+function Get-ExcludedApps{
+ if (Test-Path "$WorkingDir\excluded_apps.txt"){
+ return Get-Content -Path "$WorkingDir\excluded_apps.txt"
+ }
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Get-NotifLocal.ps1 b/Winget-AutoUpdate/functions/Get-NotifLocal.ps1
new file mode 100644
index 0000000..483edab
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Get-NotifLocal.ps1
@@ -0,0 +1,24 @@
+#Get locale file for Notification.
+
+Function Get-NotifLocal {
+ #Get OS locale
+ $OSLocale = (Get-Culture).Parent
+
+ #Test if OS locale notif file exists
+ $TestOSLocalPath = "$WorkingDir\locale\$($OSLocale.Name).xml"
+
+ #Set OS Local if file exists
+ if(Test-Path $TestOSLocalPath){
+ $LocaleDisplayName = $OSLocale.DisplayName
+ $LocaleFile = $TestOSLocalPath
+ }
+ #Set English if file doesn't exist
+ else{
+ $LocaleDisplayName = "English"
+ $LocaleFile = "$WorkingDir\locale\en.xml"
+ }
+
+ #Get locale XML file content
+ Write-Log "Notification Langugage : $LocaleDisplayName" "Cyan"
+ [xml]$Script:NotifLocale = Get-Content $LocaleFile -Encoding UTF8 -ErrorAction SilentlyContinue
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Get-WingetOutdated.ps1 b/Winget-AutoUpdate/functions/Get-WingetOutdated.ps1
new file mode 100644
index 0000000..11ff4bf
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Get-WingetOutdated.ps1
@@ -0,0 +1,75 @@
+function Get-WingetOutdated {
+ class Software {
+ [string]$Name
+ [string]$Id
+ [string]$Version
+ [string]$AvailableVersion
+ }
+
+ #Get WinGet Location
+ $WingetCmd = Get-Command winget.exe -ErrorAction SilentlyContinue
+ if ($WingetCmd){
+ $script:upgradecmd = $WingetCmd.Source
+ }
+ elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe"){
+ #WinGet < 1.17
+ $script:upgradecmd = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe" | Select-Object -ExpandProperty Path
+ }
+ elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe"){
+ #WinGet > 1.17
+ $script:upgradecmd = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe" | Select-Object -ExpandProperty Path
+ }
+ else{
+ Write-Log "Winget not installed !"
+ return
+ }
+
+ #Run winget to list apps and accept source agrements (necessary on first run)
+ & $upgradecmd list --accept-source-agreements | Out-Null
+
+ #Get list of available upgrades on winget format
+ $upgradeResult = & $upgradecmd upgrade | Out-String
+
+ #Start Convertion of winget format to an array. Check if "-----" exists
+ if (!($upgradeResult -match "-----")){
+ return
+ }
+
+ #Split winget output to lines
+ $lines = $upgradeResult.Split([Environment]::NewLine).Replace("¦ ","")
+
+ # Find the line that starts with "------"
+ $fl = 0
+ while (-not $lines[$fl].StartsWith("-----")){
+ $fl++
+ }
+
+ #Get header line
+ $fl = $fl - 2
+
+ #Get header titles
+ $index = $lines[$fl] -split '\s+'
+
+ # Line $i has the header, we can find char where we find ID and Version
+ $idStart = $lines[$fl].IndexOf($index[1])
+ $versionStart = $lines[$fl].IndexOf($index[2])
+ $availableStart = $lines[$fl].IndexOf($index[3])
+ $sourceStart = $lines[$fl].IndexOf($index[4])
+
+ # Now cycle in real package and split accordingly
+ $upgradeList = @()
+ For ($i = $fl + 2; $i -le $lines.Length; $i++){
+ $line = $lines[$i]
+ if ($line.Length -gt ($sourceStart+5) -and -not $line.StartsWith('-')){
+ $software = [Software]::new()
+ $software.Name = $line.Substring(0, $idStart).TrimEnd()
+ $software.Id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd()
+ $software.Version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd()
+ $software.AvailableVersion = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd()
+ #add formated soft to list
+ $upgradeList += $software
+ }
+ }
+
+ return $upgradeList
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Start-Init.ps1 b/Winget-AutoUpdate/functions/Start-Init.ps1
new file mode 100644
index 0000000..f7ee0b7
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Start-Init.ps1
@@ -0,0 +1,30 @@
+#Initialisation
+
+function Start-Init {
+ #Config console output encoding
+ [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
+
+ #Log Header
+ $Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format 'dd/MM/yyyy')`n##################################################"
+ $Log | Write-host
+
+ #Logs initialisation if admin
+ try{
+ $LogPath = "$WorkingDir\logs"
+ if (!(Test-Path $LogPath)){
+ New-Item -ItemType Directory -Force -Path $LogPath
+ }
+ #Log file
+ $Script:LogFile = "$LogPath\updates.log"
+ $Log | out-file -filepath $LogFile -Append
+ }
+ #Logs initialisation if non-admin
+ catch{
+ $LogPath = "$env:USERPROFILE\Winget-AutoUpdate\logs"
+ if (!(Test-Path $LogPath)){
+ New-Item -ItemType Directory -Force -Path $LogPath
+ }
+ $Script:LogFile = "$LogPath\updates.log"
+ $Log | out-file -filepath $LogFile -Append
+ }
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Start-NotifTask.ps1 b/Winget-AutoUpdate/functions/Start-NotifTask.ps1
new file mode 100644
index 0000000..feb8532
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Start-NotifTask.ps1
@@ -0,0 +1,52 @@
+function Start-NotifTask ($Title,$Message,$MessageType,$Balise) {
+
+ #Add XML variables
+ [xml]$ToastTemplate = @"
+
+
+
+ $Title
+ $Message
+
+
+
+ $Balise
+
+"@
+
+ #Check if running account is system or interactive logon
+ $currentPrincipal = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-4")
+ #if not "Interactive" user, run as system
+ if ($currentPrincipal -eq $false){
+ #Save XML to File
+ $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\"
+ if (!(Test-Path $ToastTemplateLocation)){
+ New-Item -ItemType Directory -Force -Path $ToastTemplateLocation
+ }
+ $ToastTemplate.Save("$ToastTemplateLocation\notif.xml")
+
+ #Run Notify scheduled task to notify conneted users
+ Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
+ }
+ #else, run as connected user
+ else{
+ #Load Assemblies
+ [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
+ [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
+
+ #Prepare XML
+ $ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
+ $ToastXml.LoadXml($ToastTemplate.OuterXml)
+
+ #Specify Launcher App ID
+ $LauncherID = "Windows.SystemToast.Winget.Notification"
+
+ #Prepare and Create Toast
+ $ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXml)
+ $ToastMessage.Tag = $ToastTemplate.toast.tag
+ [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)
+ }
+
+ #Wait for notification to display
+ Start-Sleep 3
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1 b/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1
new file mode 100644
index 0000000..d072075
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Start-WAUUpdateCheck.ps1
@@ -0,0 +1,32 @@
+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
new file mode 100644
index 0000000..422235a
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Test-Network.ps1
@@ -0,0 +1,36 @@
+function Test-Network {
+ #init
+ $timeout = 0
+
+ #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"
+ Write-Log "Connected !" "Green"
+ return $true
+ }
+ catch{
+ Start-Sleep 10
+ $timeout += 10
+ Write-Log "Checking internet connection. $($timeout)s." "Yellow"
+ #Send 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
+ $Message = $NotifLocale.local.outputs.output[0].message
+ $MessageType = "warning"
+ $Balise = "connection"
+ Start-NotifTask $Title $Message $MessageType $Balise
+ }
+ }
+ }
+ 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"
+ $Balise = "connection"
+ Start-NotifTask $Title $Message $MessageType $Balise
+ return $false
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Update-WAU.ps1 b/Winget-AutoUpdate/functions/Update-WAU.ps1
new file mode 100644
index 0000000..198c234
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Update-WAU.ps1
@@ -0,0 +1,66 @@
+
+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
+
+ #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","")
+ $MessageType = "info"
+ $Balise = "Winget-AutoUpdate"
+ Start-NotifTask $Title $Message $MessageType $Balise
+
+ #Run WAU update
+ try{
+ #Force to create a zip file
+ $ZipFile = "$WorkingDir\WAU_update.zip"
+ 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'
+
+ #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"
+ $TempPath = (Resolve-Path "$location\Romanitho-Winget-AutoUpdate*\Winget-AutoUpdate\").Path
+ Copy-Item -Path "$TempPath\*" -Destination "$WorkingDir\" -Recurse -Force
+
+ #Remove update zip file
+ Write-Log "Cleaning temp files"
+ Remove-Item -Path $ZipFile -Force -ErrorAction SilentlyContinue
+ #Remove update folder
+ Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue
+
+ #Set new version to conf.xml
+ [xml]$XMLconf = Get-content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
+ $XMLconf.app.version = $LatestVersion.Replace("v","")
+ $XMLconf.Save("$WorkingDir\config\about.xml")
+
+ #Send success Notif
+ $Title = $NotifLocale.local.outputs.output[3].title -f "Winget-AutoUpdate"
+ $Message = $NotifLocale.local.outputs.output[3].message -f $LatestVersion
+ $MessageType = "success"
+ $Balise = "Winget-AutoUpdate"
+ 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`""
+ exit
+ }
+ catch{
+ #Send Error Notif
+ $Title = $NotifLocale.local.outputs.output[4].title -f "Winget-AutoUpdate"
+ $Message = $NotifLocale.local.outputs.output[4].message
+ $MessageType = "error"
+ $Balise = "Winget-AutoUpdate"
+ Start-NotifTask $Title $Message $MessageType $Balise
+ Write-Log "WAU Update failed"
+ }
+}
\ No newline at end of file
diff --git a/Winget-AutoUpdate/functions/Write-Log.ps1 b/Winget-AutoUpdate/functions/Write-Log.ps1
new file mode 100644
index 0000000..8b70103
--- /dev/null
+++ b/Winget-AutoUpdate/functions/Write-Log.ps1
@@ -0,0 +1,9 @@
+
+function Write-Log ($LogMsg,$LogColor = "White") {
+ #Get log
+ $Log = "$(Get-Date -UFormat "%T") - $LogMsg"
+ #Echo log
+ $Log | Write-host -ForegroundColor $LogColor
+ #Write log to file
+ $Log | Out-File -filepath $LogFile -Append
+}
diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1
index 3a79517..8831239 100644
--- a/Winget-AutoUpdate/winget-upgrade.ps1
+++ b/Winget-AutoUpdate/winget-upgrade.ps1
@@ -1,336 +1,17 @@
-<# FUNCTIONS #>
+<# LOAD FUNCTIONS #>
-function Init {
- #Var
- $Script:WorkingDir = $PSScriptRoot
-
- #Log Header
- $Log = "##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format 'dd/MM/yyyy')`n##################################################"
- $Log | Write-host
- try{
- #Logs initialisation
- [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
- $LogPath = "$WorkingDir\logs"
- if (!(Test-Path $LogPath)){
- New-Item -ItemType Directory -Force -Path $LogPath
- }
- #Log file
- $Script:LogFile = "$LogPath\updates.log"
- $Log | out-file -filepath $LogFile -Append
- }
- catch{
- #Logs initialisation
- $LogPath = "$env:USERPROFILE\Winget-AutoUpdate\logs"
- if (!(Test-Path $LogPath)){
- New-Item -ItemType Directory -Force -Path $LogPath
- }
- $Script:LogFile = "$LogPath\updates.log"
- $Log | out-file -filepath $LogFile -Append
- }
-
- #Get locale file for Notification
- #Default english
- $DefaultLocale = "$WorkingDir\locale\en.xml"
- #Get OS locale
- $Locale = (Get-Culture).Parent
- #Test if OS locale config file exists
- $LocaleFile = "$WorkingDir\locale\$($locale.Name).xml"
- if(Test-Path $LocaleFile){
- [xml]$Script:NotifLocale = Get-Content $LocaleFile -Encoding UTF8 -ErrorAction SilentlyContinue
- $LocaleNotif = "Notification Langugage : $($locale.DisplayName)"
- }
- else{
- [xml]$Script:NotifLocale = Get-Content $DefaultLocale -Encoding UTF8 -ErrorAction SilentlyContinue
- $LocaleNotif = "Notification Langugage : English"
- }
- Write-Log $LocaleNotif "Cyan"
-}
-
-function Write-Log ($LogMsg,$LogColor = "White") {
- #Get log
- $Log = "$(Get-Date -UFormat "%T") - $LogMsg"
- #Echo log
- $Log | Write-host -ForegroundColor $LogColor
- #Write log to file
- $Log | Out-File -filepath $LogFile -Append
-}
-
-function Start-NotifTask ($Title,$Message,$MessageType,$Balise) {
-
- #Add XML variables
- [xml]$ToastTemplate = @"
-
-
-
- $Title
- $Message
-
-
-
- $Balise
-
-"@
-
- #Check if running account is system or interactive logon
- $currentPrincipal = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-4")
- #if not "Interactive" user, run as system
- if ($currentPrincipal -eq $false){
- #Save XML to File
- $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\"
- if (!(Test-Path $ToastTemplateLocation)){
- New-Item -ItemType Directory -Force -Path $ToastTemplateLocation
- }
- $ToastTemplate.Save("$ToastTemplateLocation\notif.xml")
-
- #Run Notify scheduled task to notify conneted users
- Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
- }
- #else, run as connected user
- else{
- #Load Assemblies
- [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
- [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
-
- #Prepare XML
- $ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
- $ToastXml.LoadXml($ToastTemplate.OuterXml)
-
- #Specify Launcher App ID
- $LauncherID = "Windows.SystemToast.Winget.Notification"
-
- #Prepare and Create Toast
- $ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXml)
- $ToastMessage.Tag = $ToastTemplate.toast.tag
- [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)
- }
-
- #Wait for notification to display
- Start-Sleep 3
-}
-
-function Test-Network {
- #init
- $timeout = 0
- $ping = $false
-
- #test connectivity during 30 min then timeout
- Write-Log "Checking internet connection..." "Yellow"
- while (!$ping -and $timeout -lt 1800){
- try{
- Invoke-RestMethod -Uri "https://api.github.com/zen"
- Write-Log "Connected !" "Green"
- return $true
- }
- catch{
- Start-Sleep 10
- $timeout += 10
- Write-Log "Checking internet connection. $($timeout)s." "Yellow"
- #Send Notif if no connection for 5 min
- if ($timeout -eq 300){
- Write-Log "Notify 'No connection'" "Yellow"
- $Title = $NotifLocale.local.outputs.output[0].title
- $Message = $NotifLocale.local.outputs.output[0].message
- $MessageType = "warning"
- $Balise = "connection"
- Start-NotifTask $Title $Message $MessageType $Balise
- }
- }
- }
- 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"
- $Balise = "connection"
- Start-NotifTask $Title $Message $MessageType $Balise
- return $ping
-}
-
-function Get-WingetOutdated {
- class Software {
- [string]$Name
- [string]$Id
- [string]$Version
- [string]$AvailableVersion
- }
-
- #Get WinGet Location
- $WingetCmd = Get-Command winget.exe -ErrorAction SilentlyContinue
- if ($WingetCmd){
- $script:upgradecmd = $WingetCmd.Source
- }
- elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe"){
- #WinGet < 1.17
- $script:upgradecmd = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe" | Select-Object -ExpandProperty Path
- }
- elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe"){
- #WinGet > 1.17
- $script:upgradecmd = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe" | Select-Object -ExpandProperty Path
- }
- else{
- Write-Log "Winget not installed !"
- return
- }
-
- #Run winget to list apps and accept source agrements (necessary on first run)
- & $upgradecmd list --accept-source-agreements | Out-Null
-
- #Get list of available upgrades on winget format
- $upgradeResult = & $upgradecmd upgrade | Out-String
-
- #Start Convertion of winget format to an array. Check if "-----" exists
- if (!($upgradeResult -match "-----")){
- return
- }
-
- #Split winget output to lines
- $lines = $upgradeResult.Split([Environment]::NewLine).Replace("¦ ","")
-
- # Find the line that starts with "------"
- $fl = 0
- while (-not $lines[$fl].StartsWith("-----")){
- $fl++
- }
-
- #Get header line
- $fl = $fl - 2
-
- #Get header titles
- $index = $lines[$fl] -split '\s+'
-
- # Line $i has the header, we can find char where we find ID and Version
- $idStart = $lines[$fl].IndexOf($index[1])
- $versionStart = $lines[$fl].IndexOf($index[2])
- $availableStart = $lines[$fl].IndexOf($index[3])
- $sourceStart = $lines[$fl].IndexOf($index[4])
-
- # Now cycle in real package and split accordingly
- $upgradeList = @()
- For ($i = $fl + 2; $i -le $lines.Length; $i++){
- $line = $lines[$i]
- if ($line.Length -gt ($sourceStart+5) -and -not $line.StartsWith('-')){
- $software = [Software]::new()
- $software.Name = $line.Substring(0, $idStart).TrimEnd()
- $software.Id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd()
- $software.Version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd()
- $software.AvailableVersion = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd()
- #add formated soft to list
- $upgradeList += $software
- }
- }
-
- return $upgradeList
-}
-
-function Get-ExcludedApps{
- if (Test-Path "$WorkingDir\excluded_apps.txt"){
- return Get-Content -Path "$WorkingDir\excluded_apps.txt"
- }
-}
-
-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
- }
- }
-}
-
-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
-
- #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","")
- $MessageType = "info"
- $Balise = "Winget-AutoUpdate"
- Start-NotifTask $Title $Message $MessageType $Balise
-
- #Run WAU update
- try{
- #Force to create a zip file
- $ZipFile = "$WorkingDir\WAU_update.zip"
- 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'
-
- #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"
- $TempPath = (Resolve-Path "$location\Romanitho-Winget-AutoUpdate*\Winget-AutoUpdate\").Path
- Copy-Item -Path "$TempPath\*" -Destination "$WorkingDir\" -Recurse -Force
-
- #Remove update zip file
- Write-Log "Cleaning temp files"
- Remove-Item -Path $ZipFile -Force -ErrorAction SilentlyContinue
- #Remove update folder
- Remove-Item -Path $location -Recurse -Force -ErrorAction SilentlyContinue
-
- #Set new version to conf.xml
- [xml]$XMLconf = Get-content "$WorkingDir\config\about.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
- $XMLconf.app.version = $LatestVersion.Replace("v","")
- $XMLconf.Save("$WorkingDir\config\about.xml")
-
- #Send success Notif
- $Title = $NotifLocale.local.outputs.output[3].title -f "Winget-AutoUpdate"
- $Message = $NotifLocale.local.outputs.output[3].message -f $LatestVersion
- $MessageType = "success"
- $Balise = "Winget-AutoUpdate"
- 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`""
- exit
- }
- catch{
- #Send Error Notif
- $Title = $NotifLocale.local.outputs.output[4].title -f "Winget-AutoUpdate"
- $Message = $NotifLocale.local.outputs.output[4].message
- $MessageType = "error"
- $Balise = "Winget-AutoUpdate"
- Start-NotifTask $Title $Message $MessageType $Balise
- Write-Log "WAU Update failed"
- }
-}
+#Get Working Dir
+$Script:WorkingDir = $PSScriptRoot
+#Get Functions
+Get-ChildItem "$WorkingDir\functions" | ForEach-Object {. $_.FullName}
<# MAIN #>
-#Run initialisation
-Init
+#Run log initialisation function
+Start-Init
+
+#Get Notif Locale function
+Get-NotifLocal
#Check network connectivity
if (Test-Network){