From e5bc0b663c72a8adee881a514d642cd1f332e436 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 21:44:41 +0200 Subject: [PATCH 1/6] Maybe som optimizations/millisecs... --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 61 +++++++++++++--------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 0abd987..0c868af 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -51,28 +51,25 @@ if (Test-Network) { Write-Log "WAU current version: $WAUCurrentVersion" #Check if WAU update feature is enabled or not $WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate - #If yes then check WAU update - if ($WAUDisableAutoUpdate -eq 1) { - Write-Log "WAU AutoUpdate is Disabled." "Grey" - } - else { - Write-Log "WAU AutoUpdate is Enabled." "Green" - #Get Available Version - $WAUAvailableVersion = Get-WAUAvailableVersion - #Compare - if ([version]$WAUAvailableVersion -gt [version]$WAUCurrentVersion) { - #If new version is available, update it - Write-Log "WAU Available version: $WAUAvailableVersion" "Yellow" - if ($IsSystem) { + #If yes then check WAU update if System + if ($IsSystem) { + if ($WAUDisableAutoUpdate -eq 1) { + Write-Log "WAU AutoUpdate is Disabled." "Grey" + } + else { + Write-Log "WAU AutoUpdate is Enabled." "Green" + #Get Available Version + $WAUAvailableVersion = Get-WAUAvailableVersion + #Compare + if ([version]$WAUAvailableVersion -gt [version]$WAUCurrentVersion) { + #If new version is available, update it + Write-Log "WAU Available version: $WAUAvailableVersion" "Yellow" Update-WAU } else { - Write-Log "WAU Needs to run as system to update" "Yellow" + Write-Log "WAU is up to date." "Green" } } - else { - Write-Log "WAU is up to date." "Green" - } } if ($IsSystem) { @@ -169,23 +166,23 @@ if (Test-Network) { Write-Log "No new update." "Green" } } -} + + #Run WAU in user context if currently as system + if ($IsSystem) { -#Run WAU in user context if currently as system -if ($IsSystem) { + #Get Winget system apps to excape them befor running user context + Get-WingetSystemApps - #Get Winget system apps to excape them befor running user context - Get-WingetSystemApps - - #Run user context scheduled task - $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue - if ($UserScheduledTask) { - Write-Log "Starting WAU in User context" - Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue - Exit 0 - } - else { - Write-Log "User context execution not installed" + #Run user context scheduled task + $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue + if ($UserScheduledTask) { + Write-Log "Starting WAU in User context" + Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue + Exit 0 + } + else { + Write-Log "User context execution not installed" + } } } From 21b5fa8269173e0bfed434ed847d87dd6c1c8654 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 21:58:36 +0200 Subject: [PATCH 2/6] If !$TestWinget no need to continue? --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 0c868af..9e0006c 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -165,23 +165,23 @@ if (Test-Network) { if ($InstallOK -eq 0) { Write-Log "No new update." "Green" } - } - - #Run WAU in user context if currently as system - if ($IsSystem) { - #Get Winget system apps to excape them befor running user context - Get-WingetSystemApps + #Run WAU in user context if currently as system + if ($IsSystem) { - #Run user context scheduled task - $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue - if ($UserScheduledTask) { - Write-Log "Starting WAU in User context" - Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue - Exit 0 - } - else { - Write-Log "User context execution not installed" + #Get Winget system apps to excape them befor running user context + Get-WingetSystemApps + + #Run user context scheduled task + $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue + if ($UserScheduledTask) { + Write-Log "Starting WAU in User context" + Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue + Exit 0 + } + else { + Write-Log "User context execution not installed" + } } } } From 7a560718c5c56e191e328513022077642a6eb284 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 22:10:44 +0200 Subject: [PATCH 3/6] Combined $IsSystem --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 9e0006c..0c823d6 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -49,10 +49,10 @@ if (Test-Network) { #Get Current Version $WAUCurrentVersion = $WAUConfig.DisplayVersion Write-Log "WAU current version: $WAUCurrentVersion" - #Check if WAU update feature is enabled or not - $WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate - #If yes then check WAU update if System if ($IsSystem) { + #Check if WAU update feature is enabled or not + $WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate + #If yes then check WAU update if System if ($WAUDisableAutoUpdate -eq 1) { Write-Log "WAU AutoUpdate is Disabled." "Grey" } @@ -70,10 +70,8 @@ if (Test-Network) { Write-Log "WAU is up to date." "Green" } } - } - if ($IsSystem) { - #Get External ListPath + #Get External ListPath if System if ($WAUConfig.WAU_ListPath) { Write-Log "WAU uses External Lists from: $($WAUConfig.WAU_ListPath)" $NewList = Test-ListPath $WAUConfig.WAU_ListPath $WAUConfig.WAU_UseWhiteList $WAUConfig.InstallLocation @@ -90,6 +88,7 @@ if (Test-Network) { } } } + } #Get White or Black list From deb734934d3792960763a24b713bae8c8829371b Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 22:27:03 +0200 Subject: [PATCH 4/6] Just an empty row... --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 0c823d6..61efe0e 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -88,7 +88,6 @@ if (Test-Network) { } } } - } #Get White or Black list From cc7e989e5ccfa3d751ecaee7dccd905c1aa41bf8 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 22:43:13 +0200 Subject: [PATCH 5/6] If usertask not installed, end --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 61efe0e..f5ff4ac 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -164,22 +164,17 @@ if (Test-Network) { Write-Log "No new update." "Green" } - #Run WAU in user context if currently as system - if ($IsSystem) { + #Run WAU in user context if currently as system and the user task exist + $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue + if ($IsSystem -and $UserScheduledTask) { #Get Winget system apps to excape them befor running user context Get-WingetSystemApps #Run user context scheduled task - $UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue - if ($UserScheduledTask) { - Write-Log "Starting WAU in User context" - Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue - Exit 0 - } - else { - Write-Log "User context execution not installed" - } + Write-Log "Starting WAU in User context" + Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue + Exit 0 } } } From 43de90c7cd21f4c7a5a0d17e7f564160205ebdf4 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 26 Oct 2022 23:18:42 +0200 Subject: [PATCH 6/6] Fixes --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index f5ff4ac..f584bbf 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -169,6 +169,7 @@ if (Test-Network) { if ($IsSystem -and $UserScheduledTask) { #Get Winget system apps to excape them befor running user context + Write-Log "Get list of installed Winget apps in System context..." Get-WingetSystemApps #Run user context scheduled task @@ -176,6 +177,9 @@ if (Test-Network) { Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue Exit 0 } + elseif (!$UserScheduledTask){ + Write-Log "User context execution not installed" + } } }