Exchange Logs ETL 2013/2016/2019 aufräumen mit detailliertem Loggin

The estimated reading time 4 minutes

In einer klassischen Exchange Onpremises Umgebung, haben viele die selben Probleme mit vollen Partitionen auf Grund von Logging (nicht Transaktionsprotokolle). Wie können also Exchange Installationspartitionen aufgeräumt werden?

Vor einiger Zeit stellte die alte Technet Gallery ein Bereinigungsskrit über PowerShell zur Verfügung. Leider gibt es die Technet Gallery (R.I.P.) nun nicht mehr. Das Skript kann in zwischen auf viele Websites gefunden werden, allerdings hat das Standardskrit keinerlei Logging Funktionen implementiert. Deshalb habe ich dieses um eine simple Loggingfunktion erweitert, sodass man sehen kann ob das Skript läuft und was es tut.

Zunächst mal das Originalskript:

Set-Executionpolicy RemoteSigned
$days=7
$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"


Function CleanLogfiles($TargetFolder)
{
  write-host -debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder
 
    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
    #   $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $Files = Get-ChildItem "$TargetFolder"  -Recurse | Where-Object {$_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl"}  | Where-Object {$_.lastWriteTime -le "$lastwrite"} | Select-Object FullName  
        foreach ($File in $Files)
            {
               $FullFileName = $File.FullName  
               Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; 
                Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
            }
       }
  Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
    }
}

CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

Nun folgt meine Erweiterung des Loggings, welche zunächst Logdateien schreibt und diese dann nach einer gewissen Zeit auch wieder löscht (Zeit wird ebenfalls über die Variable „$days“ definiert).

#logging section
$logpathfolder = "C:\logging"
[string]$date = Get-Date -Format "yyyy-MM-dd-HH-mm"
#protcol starting the script (never deleted)
"Script run on $date" | Out-File -FilePath "$logpathfolder\excleanup.txt" -Append
 
Start-Transcript -Path "$logpathfolder\$date-excleanup.log"
 
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
 
Stop-Transcript
  
### removing old .log files 
$fileextension = "log"
Write-Host "removing logfiles older than $days days from logpath $logpathfolder "
$limit = (Get-Date).AddDays(-$days)
$logfiles = Get-ChildItem -Path "$logpathfolder" -Include "*.$fileextension" -Recurse | Where-Object {($_.LastWriteTime -lt $limit) } 
  
foreach($log in $logfiles){
  $logname = $log.Name
  $logfilepath = $log.fullname
  Write-Host "Deleting Log file $logname, because it's older than $days" -ForegroundColor Gray
  Remove-Item -Path $logfilepath -Force -Confirm:$false 
}

Hier mal noch das komplette Skript.
HINWEIS: Bitte auf jeden Fall den Exchange Installationspfad vorher prüfen.

#Set-Executionpolicy RemoteSigned
$logpathfolder = "C:\logging"
$days=7
$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"

Function CleanLogfiles($TargetFolder)
{
  write-host -debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder
 
    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
    #   $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $Files = Get-ChildItem "$TargetFolder"  -Recurse | Where-Object {$_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl"}  | Where-Object {$_.lastWriteTime -le "$lastwrite"} | Select-Object FullName  
        foreach ($File in $Files)
            {
               $FullFileName = $File.FullName  
               Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; 
                Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
            }
       }
  Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
    }
}
 
#logging section
[string]$date = Get-Date -Format "yyyy-MM-dd-HH-mm"
#protcol starting the script (never deleted)
"Script run on $date" | Out-File -FilePath "$logpathfolder\excleanup.txt" -Append
 
Start-Transcript -Path "$logpathfolder\$date-excleanup.log"
 
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
 
Stop-Transcript
  
### removing old .log files 
$fileextension = "log"
Write-Host "removing logfiles older than $days days from logpath $logpathfolder "
$limit = (Get-Date).AddDays(-$days)
$logfiles = Get-ChildItem -Path "$logpathfolder" -Include "*.$fileextension" -Recurse | Where-Object {($_.LastWriteTime -lt $limit) } 
  
foreach($log in $logfiles){
  $logname = $log.Name
  $logfilepath = $log.fullname
  Write-Host "Deleting Log file $logname, because it's older than $days" -ForegroundColor Gray
  Remove-Item -Path $logfilepath -Force -Confirm:$false 
}

Noch ein Tipp: dieses Bereinigungsskript kann auch für andere Logging Applikationen verwendet werden. Viel Spaß beim Aufräumen der Exchange Server

Print Friendly, PDF & Email
Was this article helpful?
YesNo
0 0 votes
Article Rating
Abonnieren
Benachrichtige mich bei
guest
0 Comments
Inline Feedbacks
View all comments