2024-09-02 22:23:24 +00:00
|
|
|
#Function to rotate the logs
|
2023-01-12 20:16:03 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
|
2024-09-02 20:58:12 +00:00
|
|
|
|
|
|
|
# if MaxLogFiles is 1 just keep the original one and let it grow
|
|
|
|
if (-not($MaxLogFiles -eq 1)) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
# get current size of log file
|
|
|
|
$currentSize = (Get-Item $LogFile).Length
|
|
|
|
|
|
|
|
# get log name
|
|
|
|
$logFileName = Split-Path $LogFile -Leaf
|
|
|
|
$logFilePath = Split-Path $LogFile
|
|
|
|
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
|
|
|
|
$logFileNameExtension = [System.IO.Path]::GetExtension($logFileName)
|
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
if ($currentSize -ge $MaxLogSize) {
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
# construct name of archived log file
|
|
|
|
$newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension
|
2024-09-02 22:20:04 +00:00
|
|
|
# rename old log file
|
2024-09-02 20:58:12 +00:00
|
|
|
Rename-Item -Path $LogFile -NewName $newLogFileName -Force -Confirm:$false
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2024-09-02 20:58:12 +00:00
|
|
|
# create new file
|
|
|
|
Write-ToLog "New log file created"
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
# if MaxLogFiles is 0 don't delete any old archived log files
|
|
|
|
if (-not($MaxLogFiles -eq 0)) {
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
# set filter to search for archived log files
|
|
|
|
$archivedLogFileFilter = $logFileNameWithoutExtension + '??????????????' + $logFileNameExtension
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
# get archived log files
|
|
|
|
$oldLogFiles = Get-Item -Path "$(Join-Path -Path $logFilePath -ChildPath $archivedLogFileFilter)"
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
if ([bool]$oldLogFiles) {
|
|
|
|
# compare found log files to MaxLogFiles parameter of the log object, and delete oldest until we are
|
|
|
|
# back to the correct number
|
|
|
|
if (($oldLogFiles.Count + 1) -gt $MaxLogFiles) {
|
|
|
|
[int]$numTooMany = (($oldLogFiles.Count) + 1) - $MaxLogFiles
|
|
|
|
$oldLogFiles | Sort-Object 'LastWriteTime' | Select-Object -First $numTooMany | Remove-Item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
#Log Header
|
2023-10-20 14:27:29 +00:00
|
|
|
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader
|
|
|
|
Write-ToLog -LogMsg "Max Log Size reached: $MaxLogSize bytes - Rotated Logs"
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
Return $True
|
|
|
|
}
|
|
|
|
}
|
2024-09-02 20:58:12 +00:00
|
|
|
|
|
|
|
catch {
|
|
|
|
Return $False
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
}
|
2024-09-02 20:58:12 +00:00
|
|
|
|
2023-01-12 20:16:03 +00:00
|
|
|
}
|