@@ -11,25 +11,23 @@ function Invoke-LogRotation {
1111 # if MaxLogFiles is 1 just keep the original one and let it grow
1212 if (-not ($MaxLogFiles -eq 1 )) {
1313 try {
14- # get current size of log file
14+ # get current size of standard log file
1515 $currentSize = (Get-Item $LogFile ).Length
1616
17- # get log name
17+ # get standard log name
1818 $logFileName = Split-Path $LogFile - Leaf
1919 $logFilePath = Split-Path $LogFile
2020 $logFileNameWithoutExtension = [System.IO.Path ]::GetFileNameWithoutExtension($logFileName )
2121 $logFileNameExtension = [System.IO.Path ]::GetExtension($logFileName )
2222
2323 if ($currentSize -ge $MaxLogSize ) {
24+ $logrotate = $true
2425
2526 # construct name of archived log file
2627 $newLogFileName = $logFileNameWithoutExtension + (Get-Date - Format ' yyyyMMddHHmmss' ).ToString() + $logFileNameExtension
2728 # rename old log file
2829 Rename-Item - Path $LogFile - NewName $newLogFileName - Force - Confirm:$false
2930
30- # create new file
31- Write-ToLog " New log file created"
32-
3331 # if MaxLogFiles is 0 don't delete any old archived log files
3432 if (-not ($MaxLogFiles -eq 0 )) {
3533
@@ -48,11 +46,58 @@ function Invoke-LogRotation {
4846 }
4947 }
5048 }
49+ }
50+
51+ # CM log file name
52+ $CMLogFile = $LogFile -replace " \.log$" , " _CM.log"
53+
54+ # get current size of CM log file if it exists
55+ if (Test-Path $CMLogFile ) {
56+ $currentCMSize = (Get-Item $CMLogFile ).Length
57+
58+ # get CM log name
59+ $logFileName = Split-Path $CMLogFile - Leaf
60+ $logFilePath = Split-Path $CMLogFile
61+ $logFileNameWithoutExtension = [System.IO.Path ]::GetFileNameWithoutExtension($logFileName )
62+ $logFileNameExtension = [System.IO.Path ]::GetExtension($logFileName )
63+
64+ if ($currentCMSize -ge $MaxLogSize ) {
65+ $CM_logrotate = $true
66+
67+ # construct name of archived log file
68+ $newLogFileName = $logFileNameWithoutExtension + (Get-Date - Format ' yyyyMMddHHmmss' ).ToString() + $logFileNameExtension
69+ # rename old log file
70+ Rename-Item - Path $CMLogFile - NewName $newLogFileName - Force - Confirm:$false
71+
72+ # if MaxLogFiles is 0 don't delete any old archived log files
73+ if (-not ($MaxLogFiles -eq 0 )) {
74+
75+ # set filter to search for archived log files
76+ $archivedLogFileFilter = $logFileNameWithoutExtension + ' ??????????????' + $logFileNameExtension
77+
78+ # get archived log files
79+ $oldLogFiles = Get-Item - Path " $ ( Join-Path - Path $logFilePath - ChildPath $archivedLogFileFilter ) "
80+
81+ if ([bool ]$oldLogFiles ) {
82+ # compare found log files to MaxLogFiles parameter of the log object, and delete oldest until we are
83+ # back to the correct number
84+ if (($oldLogFiles.Count + 1 ) -gt $MaxLogFiles ) {
85+ [int ]$numTooMany = (($oldLogFiles.Count ) + 1 ) - $MaxLogFiles
86+ $oldLogFiles | Sort-Object ' LastWriteTime' | Select-Object - First $numTooMany | Remove-Item
87+ }
88+ }
89+ }
90+ }
91+ }
5192
52- # Log Header
53- Write-ToLog - LogMsg " CHECK FOR APP UPDATES (System context) " - IsHeader
54- Write-ToLog - LogMsg " Max Log Size reached: $MaxLogSize bytes - Rotated Logs"
93+ # Log actions
94+ if ( $logrotate ) {
95+ Write-ToLog " ### Max Standard log size reached: $MaxLogSize bytes - Rotated Logs ### "
5596 }
97+ if ($CM_logrotate ) {
98+ Write-ToLog " ### Max CM log size reached: $MaxLogSize bytes - Rotated CM Logs ###"
99+ }
100+
56101 # end of try block
57102 Return $true ;
58103 }
0 commit comments