From 4d0fc6e445b27de0ba47f0560525f803624a8083 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Fri, 13 Jan 2023 06:32:57 +0100 Subject: [PATCH] GPO and fixes for Max-Logfiles/LogSize --- Policies/WAU.admx | 14 ++++++++ Policies/en-US/WAU.adml | 23 ++++++++++++- Winget-AutoUpdate/Winget-Upgrade.ps1 | 34 ++++++++++++++++++- Winget-AutoUpdate/functions/Get-Policies.ps1 | 21 ++++++++++-- .../functions/Invoke-LogRotation.ps1 | 1 + Winget-AutoUpdate/functions/Start-Init.ps1 | 23 ------------- 6 files changed, 89 insertions(+), 27 deletions(-) diff --git a/Policies/WAU.admx b/Policies/WAU.admx index a5754b3..c4aa293 100644 --- a/Policies/WAU.admx +++ b/Policies/WAU.admx @@ -319,5 +319,19 @@ + + + + + + + + + + + + + + diff --git a/Policies/en-US/WAU.adml b/Policies/en-US/WAU.adml index 4952d15..f3a10c7 100644 --- a/Policies/en-US/WAU.adml +++ b/Policies/en-US/WAU.adml @@ -98,7 +98,7 @@ If this policy is not configured or disabled, Updates at Time: (06:00 AM).23:00 24:00 User context execution - This policy setting specifies whether to enable User context execution or not . + This policy setting specifies whether to enable User context execution or not. If this policy is disabled or not configured, the default is No. Enable Deskop Shortcut @@ -113,6 +113,17 @@ WAU - Open logs WAU - Web Help If this policy is disabled or not configured, the default is No. + Log: Number of allowed log files + If this policy is enabled, you can set a number of allowed log files: +Setting MaxLogFiles to 0 don't delete any old archived log files, 1 keeps the original one and just let it grow. +Default number is 3 (0-99) + +If this policy is disabled or not configured, the default number is used. + Log: Size of the log file in bytes before rotating + If this policy is enabled, you can set the size of the log file in bytes before rotating. +Default size is 1048576 = 1 MB + +If this policy is disabled or not configured, the default size is used. @@ -134,6 +145,16 @@ If this policy is disabled or not configured, the default is No. + + + + + + + + + + diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index c137ad6..b3b01bc 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -22,7 +22,39 @@ if ($IsSystem) { Write-Log "Running in System context" #Get and set Domain/Local Policies (GPO) - Get-Policies + $ChangedSettings = Get-Policies + + # Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files. + $MaxLogFiles = $WAUConfig.WAU_MaxLogFiles + if ($null -eq $MaxLogFiles) { + [int32] $MaxLogFiles = 3 + } + else { + [int32] $MaxLogFiles = $MaxLogFiles + } + + # Maximum size of log file. + $MaxLogSize = $WAUConfig.WAU_MaxLogSize + if (!$MaxLogSize) { + [int64] $MaxLogSize = 1048576 # in bytes, default is 1048576 = 1 MB + } + else { + [int64] $MaxLogSize = $MaxLogSize + } + + #LogRotation if System + $Rotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize + if ($Rotate) { + #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) { + Write-Log "Activated WAU GPO Management detected, comparing..." + Write-Log "Changed settings: $ChangedSettings" "Yellow" + } + } #Run post update actions if necessary if run as System if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) { diff --git a/Winget-AutoUpdate/functions/Get-Policies.ps1 b/Winget-AutoUpdate/functions/Get-Policies.ps1 index 258fb62..ff808f0 100644 --- a/Winget-AutoUpdate/functions/Get-Policies.ps1 +++ b/Winget-AutoUpdate/functions/Get-Policies.ps1 @@ -329,12 +329,29 @@ Function Get-Policies { $ChangedSettings++ } + if ($null -ne $($WAUPolicies.WAU_MaxLogFiles) -and ($($WAUPolicies.WAU_MaxLogFiles) -ne $($WAUConfig.WAU_MaxLogFiles))) { + New-ItemProperty $regPath -Name WAU_MaxLogFiles -Value $($WAUPolicies.WAU_MaxLogFiles.TrimEnd(" ", "\", "/")) -Force | Out-Null + $ChangedSettings++ + } + elseif ($null -eq $($WAUPolicies.WAU_MaxLogFiles) -and $($WAUConfig.WAU_MaxLogFiles) -ne 3) { + New-ItemProperty $regPath -Name WAU_MaxLogFiles -Value 3 -Force | Out-Null + $ChangedSettings++ + } + + if ($null -ne $($WAUPolicies.WAU_MaxLogSize) -and ($($WAUPolicies.WAU_MaxLogSize) -ne $($WAUConfig.WAU_MaxLogSize))) { + New-ItemProperty $regPath -Name WAU_MaxLogSize -Value $($WAUPolicies.WAU_MaxLogSize.TrimEnd(" ", "\", "/")) -Force | Out-Null + $ChangedSettings++ + } + elseif ($null -eq $($WAUPolicies.WAU_MaxLogSize) -and $($WAUConfig.WAU_MaxLogSize) -ne 1048576) { + New-ItemProperty $regPath -Name WAU_MaxLogSize -Value 1048576 -Force | Out-Null + $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 + Return $ChangedSettings } diff --git a/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 b/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 index ca33fdf..d63f8bb 100644 --- a/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1 @@ -69,6 +69,7 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) { } } } + Return $True } } } diff --git a/Winget-AutoUpdate/functions/Start-Init.ps1 b/Winget-AutoUpdate/functions/Start-Init.ps1 index 6ea3272..280ecbb 100644 --- a/Winget-AutoUpdate/functions/Start-Init.ps1 +++ b/Winget-AutoUpdate/functions/Start-Init.ps1 @@ -5,24 +5,6 @@ function Start-Init { #Config console output encoding [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 - # Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files. - $MaxLogFiles = Get-ItemPropertyvalue -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate -Name "WAU_MaxLogFiles" -ErrorAction SilentlyContinue - if ($null = $MaxLogFiles) { - [int32] $MaxLogFiles = 3 - } - else { - [int32] $MaxLogFiles = $MaxLogFiles - } - - # Maximum size of log file. - $MaxLogSize = Get-ItemPropertyvalue -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate -Name "WAU_MaxLogSize" -ErrorAction SilentlyContinue - if (!$MaxLogSize) { - [int64] $MaxLogSize = 1048576 # in bytes, default is 1048576 = 1 MB - } - else { - [int64] $MaxLogSize = $MaxLogSize - } - #Log Header $Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" $Log | Write-host @@ -45,11 +27,6 @@ function Start-Init { Set-Acl -Path $LogFile -AclObject $NewAcl } - #LogRotation if System - if ($IsSystem) { - Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize - } - #Log file $Log | out-file -filepath $LogFile -Append