From c4834c0053601bfee0890a18f948b3c764f66aff Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Fri, 13 Jan 2023 21:42:06 +0100 Subject: [PATCH] Optimized logging of Rotation/Policies --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 17 ++++++++++++----- Winget-AutoUpdate/functions/Get-Policies.ps1 | 5 +---- .../functions/Invoke-LogRotation.ps1 | 7 +++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index b3b01bc..94f8124 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -22,7 +22,11 @@ if ($IsSystem) { Write-Log "Running in System context" #Get and set Domain/Local Policies (GPO) - $ChangedSettings = Get-Policies + $ActivateGPOManagement, $ChangedSettings = Get-Policies + if ($null -ne $ChangedSettings -and $ActivateGPOManagement) { + Write-Log "Activated WAU GPO Management detected, comparing..." + Write-Log "Changed settings: $ChangedSettings" "Yellow" + } # Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files. $MaxLogFiles = $WAUConfig.WAU_MaxLogFiles @@ -43,17 +47,20 @@ if ($IsSystem) { } #LogRotation if System - $Rotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize - if ($Rotate) { + $Exception, $Rotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize + if ($Exception -eq $True) { + Write-Log "An Exception occured during Log Rotation..." + } + if ($Rotate -eq $True) { #Log Header $Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" - $Log | Write-host $Log | out-file -filepath $LogFile -Append Write-Log "Running in System context" - if ($null -ne $ChangedSettings) { + if ($null -ne $ChangedSettings -and $ActivateGPOManagement) { Write-Log "Activated WAU GPO Management detected, comparing..." Write-Log "Changed settings: $ChangedSettings" "Yellow" } + Write-Log "Max Log Size reached: $MaxLogSize bytes - Rotated Logs" } #Run post update actions if necessary if run as System diff --git a/Winget-AutoUpdate/functions/Get-Policies.ps1 b/Winget-AutoUpdate/functions/Get-Policies.ps1 index ff808f0..007593a 100644 --- a/Winget-AutoUpdate/functions/Get-Policies.ps1 +++ b/Winget-AutoUpdate/functions/Get-Policies.ps1 @@ -6,7 +6,6 @@ Function Get-Policies { if ($WAUPolicies) { if ($($WAUPolicies.WAU_ActivateGPOManagement -eq 1)) { $ChangedSettings = 0 - Write-Log "Activated WAU GPO Management detected, comparing..." $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" if ($null -ne $($WAUPolicies.WAU_BypassListForUsers) -and ($($WAUPolicies.WAU_BypassListForUsers) -ne $($WAUConfig.WAU_BypassListForUsers))) { New-ItemProperty $regPath -Name WAU_BypassListForUsers -Value $($WAUPolicies.WAU_BypassListForUsers) -PropertyType DWord -Force | Out-Null @@ -347,11 +346,9 @@ Function Get-Policies { $ChangedSettings++ } - Write-Log "Changed settings: $ChangedSettings" "Yellow" - #Get WAU Configurations after Policies change $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" } } - Return $ChangedSettings + Return $($WAUPolicies.WAU_ActivateGPOManagement), $ChangedSettings } diff --git a/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 b/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 index d63f8bb..66dd49e 100644 --- a/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 @@ -48,7 +48,7 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) { Set-Acl -Path $LogFile -AclObject $NewAcl } catch { - Write-Log $_.Exception.Message + Return $True, $False } # if MaxLogFiles is 0 don't delete any old archived log files @@ -69,12 +69,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) { } } } - Return $True + Return $False, $True } } } catch { - Write-Log "`n`n##################################################`n# CHECK FOR LOG ROTATION - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" - Write-Log $_.Exception.Message + Return $True, $False } }