From e2c446a2be919d3c0f29ca19cde2ec36eb4cdbbf Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 28 Dec 2022 17:28:22 +0100 Subject: [PATCH] Optimized if run as System... --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 37 +++++++++++----------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index c01d3b8..5a54b7e 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -14,18 +14,12 @@ $Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsS #Run log initialisation function Start-Init -#Log running context -if ($IsSystem) { - Write-Log "Running in System context" -} -else { - Write-Log "Running in User context" -} - #Get WAU Configurations $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" +#Log running context and more... if ($IsSystem) { + Write-Log "Running in System context" #Get WAU Policies and set the Configurations Registry Accordingly $WAUPolicies = Get-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate" -ErrorAction SilentlyContinue if ($WAUPolicies) { @@ -89,18 +83,17 @@ if ($IsSystem) { } } } -} - -#Run post update actions if necessary -if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) { - Invoke-PostUpdateActions -} - -#Run Scope Machine funtion if run as system -if ($IsSystem) { + #Run post update actions if necessary if run as System + if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) { + Invoke-PostUpdateActions + } + #Run Scope Machine funtion if run as System $SettingsPath = "$Env:windir\system32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\defaultState\settings.json" Add-ScopeMachine $SettingsPath } +else { + Write-Log "Running in User context" +} #Get Notif Locale function $LocaleDisplayName = Get-NotifLocale @@ -116,9 +109,9 @@ if (Test-Network) { $WAUCurrentVersion = $WAUConfig.DisplayVersion Write-Log "WAU current version: $WAUCurrentVersion" if ($IsSystem) { - #Check if WAU update feature is enabled or not + #Check if WAU update feature is enabled or not if run as System $WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate - #If yes then check WAU update if System + #If yes then check WAU update if run as System if ($WAUDisableAutoUpdate -eq 1) { Write-Log "WAU AutoUpdate is Disabled." "Grey" } @@ -137,12 +130,12 @@ if (Test-Network) { } } - #Delete previous list_/winget_error (if they exist) if System + #Delete previous list_/winget_error (if they exist) if run as System if (Test-Path "$WorkingDir\logs\error.txt") { Remove-Item "$WorkingDir\logs\error.txt" -Force } - #Get External ListPath if System + #Get External ListPath if run as System if ($WAUConfig.WAU_ListPath) { Write-Log "WAU uses External Lists from: $($WAUConfig.WAU_ListPath.TrimEnd(" ", "\", "/"))" $NewList = Test-ListPath $WAUConfig.WAU_ListPath.TrimEnd(" ", "\", "/") $WAUConfig.WAU_UseWhiteList $WAUConfig.InstallLocation.TrimEnd(" ", "\") @@ -161,7 +154,7 @@ if (Test-Network) { } } - #Get External ModsPath if System + #Get External ModsPath if run as System if ($WAUConfig.WAU_ModsPath) { Write-Log "WAU uses External Mods from: $($WAUConfig.WAU_ModsPath.TrimEnd(" ", "\", "/"))" $NewMods, $DeletedMods = Test-ModsPath $WAUConfig.WAU_ModsPath.TrimEnd(" ", "\", "/") $WAUConfig.InstallLocation.TrimEnd(" ", "\")