Public/ps1/Log/Log-ApprxrManagement.ps1
|
<#
.SYNOPSIS Manages log file retention and rotation for Apprxr logs. .DESCRIPTION The Log-ApprxrManagement function checks the log file's last write time and rotates the log if it is older than the configured retention period. It also enforces the maximum number of log files by deleting the oldest log file if the retention count is exceeded. .EXAMPLE Log-ApprxrManagement .NOTES This function is intended to be called periodically to ensure log files do not grow indefinitely and to maintain log retention policies. #> function Log-ApprxrManagement { # Ensure log management configuration is loaded if ($LogManagement) { Load-ApprxrLogConfiguration } $logFile = $LogManagement.LogFolder # Get the last write time of the log file $lastWrite = (get-item $logFile).LastWriteTime $timespan = new-timespan -hours ($LogManagement.Hours) # Rotate the log file if it is older than the retention period if (((get-date) - $lastWrite) -gt $timespan) { # Log file is older than retention period, rotate it Move-LogFile } else { # Log file is within retention period, do nothing } # Enforce log file retention count if ((Get-ChildItem ($logFile+"*") | Measure-Object).Count -gt $LogManagement.LogRetention) { $files = Get-ChildItem ($logFile+"*") # Find the oldest rotated log file $file = ($files.Name | Where-Object {$_.Contains("_")} |Sort-Object {$_})[0] $fileToDelete = Get-ChildItem ($logFile+"_"+$file.Split("_")[1]) # Delete the oldest log file to maintain retention Remove-Item $fileToDelete.FullName } } |