GPO and fixes for Max-Logfiles/LogSize

pull/257/head
KnifMelti 2023-01-13 06:32:57 +01:00
parent bc7dd12a60
commit 4d0fc6e445
6 changed files with 89 additions and 27 deletions

View File

@ -319,5 +319,19 @@
<decimal value="0" /> <decimal value="0" />
</disabledValue> </disabledValue>
</policy> </policy>
<policy name="MaxLogFiles_Name" class="Machine" displayName="$(string.MaxLogFiles_Name)" explainText="$(string.MaxLogFiles_Explain)" key="Software\Policies\Romanitho\Winget-AutoUpdate" presentation="$(presentation.MaxLogFiles)" >
<parentCategory ref="WAU"/>
<supportedOn ref="WAU:SUPPORTED_WAU_1_15_3"/>
<elements>
<text id="MaxLogFiles" valueName="WAU_MaxLogFiles" />
</elements>
</policy>
<policy name="MaxLogSize_Name" class="Machine" displayName="$(string.MaxLogSize_Name)" explainText="$(string.MaxLogSize_Explain)" key="Software\Policies\Romanitho\Winget-AutoUpdate" presentation="$(presentation.MaxLogSize)" >
<parentCategory ref="WAU"/>
<supportedOn ref="WAU:SUPPORTED_WAU_1_15_3"/>
<elements>
<text id="MaxLogSize" valueName="WAU_MaxLogSize" />
</elements>
</policy>
</policies> </policies>
</policyDefinitions> </policyDefinitions>

View File

@ -113,6 +113,17 @@ WAU - Open logs
WAU - Web Help WAU - Web Help
If this policy is disabled or not configured, the default is No.</string> If this policy is disabled or not configured, the default is No.</string>
<string id="MaxLogFiles_Name">Log: Number of allowed log files</string>
<string id="MaxLogFiles_Explain">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.</string>
<string id="MaxLogSize_Name">Log: Size of the log file in bytes before rotating</string>
<string id="MaxLogSize_Explain">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.</string>
</stringTable> </stringTable>
<presentationTable> <presentationTable>
<presentation id="ListPath"> <presentation id="ListPath">
@ -134,6 +145,16 @@ If this policy is disabled or not configured, the default is No.</string>
<presentation id="UpdatesAtTime"> <presentation id="UpdatesAtTime">
<dropdownList refId="UpdatesAtTime"/> <dropdownList refId="UpdatesAtTime"/>
</presentation> </presentation>
<presentation id="MaxLogFiles">
<textBox refId="MaxLogFiles">
<label>Allowed log files:</label>
</textBox>
</presentation>
<presentation id="MaxLogSize">
<textBox refId="MaxLogSize">
<label>Size of the log file:</label>
</textBox>
</presentation>
</presentationTable> </presentationTable>
</resources> </resources>
</policyDefinitionResources> </policyDefinitionResources>

View File

@ -22,7 +22,39 @@ if ($IsSystem) {
Write-Log "Running in System context" Write-Log "Running in System context"
#Get and set Domain/Local Policies (GPO) #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 #Run post update actions if necessary if run as System
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) { if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {

View File

@ -329,12 +329,29 @@ Function Get-Policies {
$ChangedSettings++ $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" Write-Log "Changed settings: $ChangedSettings" "Yellow"
#Get WAU Configurations after Policies change #Get WAU Configurations after Policies change
$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
} }
} }
Return $ChangedSettings
Return
} }

View File

@ -69,6 +69,7 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
} }
} }
} }
Return $True
} }
} }
} }

View File

@ -5,24 +5,6 @@ function Start-Init {
#Config console output encoding #Config console output encoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [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 Header
$Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################" $Log = "`n##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n##################################################"
$Log | Write-host $Log | Write-host
@ -45,11 +27,6 @@ function Start-Init {
Set-Acl -Path $LogFile -AclObject $NewAcl Set-Acl -Path $LogFile -AclObject $NewAcl
} }
#LogRotation if System
if ($IsSystem) {
Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
}
#Log file #Log file
$Log | out-file -filepath $LogFile -Append $Log | out-file -filepath $LogFile -Append