diff --git a/CHANGELOG.md b/CHANGELOG.md index 500ac95..7033259 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.3.1] - 2026-06-29 + +### Fixed + +- `Clear-SPSLog` no longer filters on an undefined `$logFileName` variable (which + resolved to `*`, accidentally working but never targeting `.log` files + specifically). It now takes a `-Filter` parameter (default `*.log`) and honors + `-Retention 0` to disable pruning (#42). + +### Changed + +- Log retention is no longer hardcoded to 180 days: new config setting + `LogRetentionDays` (default 180, 0 disables pruning), mirroring + `JsonHistoryRetentionDays` (#42). + ## [2.3.0] - 2026-06-29 ### Added diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 39a4cce..d3ff206 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,5 +1,19 @@ # SPSWeather - Release Notes +## [2.3.1] - 2026-06-29 + +### Fixed + +- `Clear-SPSLog` now actually targets `.log` files (it was filtering on an + undefined variable). Retention 0 disables pruning. + +### Changed + +- New config setting `LogRetentionDays` (default 180, 0 disables) replaces the + hardcoded 180-day retention, mirroring `JsonHistoryRetentionDays`. + +A full list of changes can be found in the [change log](CHANGELOG.md). + ## [2.3.0] - 2026-06-29 ### Added diff --git a/src/Config/CONTOSO-PROD.example.psd1 b/src/Config/CONTOSO-PROD.example.psd1 index df5f76e..4471a98 100644 --- a/src/Config/CONTOSO-PROD.example.psd1 +++ b/src/Config/CONTOSO-PROD.example.psd1 @@ -52,6 +52,10 @@ # pruning. Default: 30. JsonHistoryRetentionDays = 30 + # LogRetentionDays: how many days of *.log transcript files are kept under + # Logs\ by Clear-SPSLog. 0 disables pruning. Default: 180. + LogRetentionDays = 180 + # Farms : one entry per trusted farm to check. Server is the short name; the # Domain above is appended to build the FQDN targeted for remoting. SqlServers # is OPTIONAL: the SQL client alias(es) (cliconfg) or server name(s) this farm diff --git a/src/Modules/SPSWeather.Common/Public/Clear-SPSLog.ps1 b/src/Modules/SPSWeather.Common/Public/Clear-SPSLog.ps1 index a77e687..21f02df 100644 --- a/src/Modules/SPSWeather.Common/Public/Clear-SPSLog.ps1 +++ b/src/Modules/SPSWeather.Common/Public/Clear-SPSLog.ps1 @@ -6,35 +6,34 @@ [Parameter()] [System.UInt32] - $Retention = 180 + $Retention = 180, + + [Parameter()] + [System.String] + $Filter = '*.log' ) + if ($Retention -eq 0) { + Write-Verbose -Message "Clear-SPSLog: retention is 0, pruning disabled." + return + } + if (Test-Path $path) { - # Get the current date $Now = Get-Date - # Define LastWriteTime parameter based on $days $LastWrite = $Now.AddDays(-$Retention) - # Get files based on lastwrite filter and specified folder - $files = Get-Childitem -Path $path -Filter "$($logFileName)*" | Where-Object -FilterScript { - $_.LastWriteTime -le "$LastWrite" - } + $files = Get-Childitem -Path $path -Filter $Filter -File -ErrorAction SilentlyContinue | + Where-Object -FilterScript { $_.LastWriteTime -le $LastWrite } if ($files) { Write-Output '--------------------------------------------------------------' - Write-Output "Cleaning log files in $path ..." + Write-Output "Cleaning files matching '$Filter' in $path (older than $Retention days) ..." foreach ($file in $files) { - if ($null -ne $file) { - Write-Output "Deleting file $file ..." - Remove-Item $file.FullName | out-null - } - else { - Write-Output 'No more log files to delete' - Write-Output '--------------------------------------------------------------' - } + Write-Output "Deleting file $($file.FullName) ..." + Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue } } else { Write-Output '--------------------------------------------------------------' - Write-Output "$path - No needs to delete log files" + Write-Output "$path - No needs to delete files matching '$Filter'" Write-Output '--------------------------------------------------------------' } } diff --git a/src/Modules/SPSWeather.Common/SPSWeather.Common.psd1 b/src/Modules/SPSWeather.Common/SPSWeather.Common.psd1 index cbbe5bc..1e832a9 100644 --- a/src/Modules/SPSWeather.Common/SPSWeather.Common.psd1 +++ b/src/Modules/SPSWeather.Common/SPSWeather.Common.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'SPSWeather.Common.psm1' - ModuleVersion = '2.3.0' + ModuleVersion = '2.3.1' GUID = 'c39bd612-8520-4e65-9037-80060894d654' Author = 'Jean-Cyril DROUHIN' CompanyName = 'luigilink' diff --git a/src/SPSWeather.ps1 b/src/SPSWeather.ps1 index 2089b65..ad9d37a 100644 --- a/src/SPSWeather.ps1 +++ b/src/SPSWeather.ps1 @@ -216,6 +216,7 @@ else { $sqlDiskThreshold = if ($null -ne $envCfg.SQLDiskFreeThresholdPercent) { [int]$envCfg.SQLDiskFreeThresholdPercent } else { 15 } $sqlBackupMaxAge = if ($null -ne $envCfg.SQLBackupMaxAgeDays) { [int]$envCfg.SQLBackupMaxAgeDays } else { 3 } $jsonHistoryRetentionDays = if ($null -ne $envCfg.JsonHistoryRetentionDays) { [int]$envCfg.JsonHistoryRetentionDays } else { 30 } + $logRetentionDays = if ($null -ne $envCfg.LogRetentionDays) { [int]$envCfg.LogRetentionDays } else { 180 } $pathHistoryFolder = Join-Path -Path $pathResultsFolder -ChildPath 'history' Add-SPSWeatherEvent -Message "SPSWeather $spsWeatherVersion started for $Application/$Environment on $env:COMPUTERNAME." -EntryType 'Information' -EventID 1000 foreach ($spFarm in $spFarms) { @@ -473,7 +474,7 @@ else { } # Clean the folder of log files - Clear-SPSLog -path $pathLogsFolder -Retention 180 + Clear-SPSLog -path $pathLogsFolder -Retention $logRetentionDays # Send Email if ($EnableSmtp) { diff --git a/tests/SPSWeather.Common.Tests.ps1 b/tests/SPSWeather.Common.Tests.ps1 index e75c8a0..e9850da 100644 --- a/tests/SPSWeather.Common.Tests.ps1 +++ b/tests/SPSWeather.Common.Tests.ps1 @@ -36,7 +36,7 @@ Describe 'SPSWeather.Common module' { } It 'manifest version is 2.0.0 or higher' { - (Test-ModuleManifest -Path $modulePath).Version | Should -BeGreaterOrEqual ([version]'2.3.0') + (Test-ModuleManifest -Path $modulePath).Version | Should -BeGreaterOrEqual ([version]'2.3.1') } It 'exports exactly the expected public functions' {