Problem
Once you deploy on-premises Exchange, the available disk space will decrease due to the increase in logs generated by the mailboxes. This problem of large log sizes is predominant in Exchange versions 2013 and above.
When you install RecoveryManager Plus to back up your on-premises Exchange mailboxes, the product uses the Exchange Web Services (EWS) API to access mailboxes located on the Exchange Server. Every time the product accesses these APIs for any action like taking a backup or performing a restoration, Microsoft Exchange creates a log. If your environment has a large number of mailboxes and you have scheduled daily incremental backups, the available disk space will reduce quickly.
In addition to logs generated by the server when a backup operation is run, there are plenty of logs that are generated on a daily basis by the Exchange server. Unless you regularly clean up old log files, it is very common for mailbox databases to automatically unmount and cause disruptions to your users with the error 452 4.3.1 Insufficient system resources error.
The most efficient way in which this problem can be solved is by periodically clearing old logs created by the Exchange server due to backups and other normal activity. Since these logs are created by the Microsoft Exchange server, these cannot be deleted by RecoveryManager Plus.
This article will elucidate how you can automate clearing any transaction log files.
Disclaimer: Please be aware that RecoveryManager Plus does not directly create any logs in your Exchange server nor does it delete existing logs. This article explains how you can automatically clear logs that have been and will be created by your Exchange server.
Step 1: Creating the required PowerShell script
Create a PowerShell script.
# Set execution policy if not set
$ExecutionPolicy = Get-ExecutionPolicy
if ($ExecutionPolicy -ne "RemoteSigned") {
Set-ExecutionPolicy RemoteSigned -Force
}
# Cleanup logs older than the set of days in numbers
$days = 2
# Path of the logs that you like to cleanup
$LogPaths = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\Ews\"
# Clean the logs
Function CleanLogfiles($TargetFolders) {
foreach ($TargetFolder in $TargetFolders) {
Write-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder
if (Test-Path $TargetFolder) {
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like "*.log" }
| Where-Object { $_.lastWriteTime -ge "$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($LogPaths)
Note 1: In this sample script, we have provided the value 2 in line 7 of the script. Depending on the value provided here, logs older than the number of days of the provided value will be deleted.You can modify the value to any number depending on your incremental backup frequency configured. If you have a daily scheduler, we recommend you provide the value 1. Likewise, use the values 7 and 31 if your incremental backup frequency is weekly and monthly, respectively.
Note 2: If you have installed Exchange in a different directory instead of the default path, provide the path in line 10 of the script. Depending on the version of your Exchange Server, the value ../V15/... has to be changed to the relevant number. If you want to periodically clear other logs in your system, add the location of the logs at the end of $LogPaths separated by a comma.
Store the script as CleanupLogs.ps1 and place it in the C:\Scripts\ folder. If you do not have a Scripts folder, create one in the C:\ drive.
Start Task Scheduler
Click on Task Scheduler Library from the left-pane and click New folder from the right-pane. Name the folder as RecoveryManager Plus and click OK.
Click the newly created Exchange folder from the left-pane and click Create Task from the right-pane.
A Create Task window pops-up.
Click on the General tab.
In the Name field, enter EWSLogsCleanup.
Under Security options,
Click on the Change User or Group button and select SYSTEM.
Enable Run with highest privileges.
Select Configure for Windows Server 2016 if you have a Windows Server 2016. If you have a different Windows Server version, select the appropriate one.
Click on the Triggers tab and click New.
In the Settings section, select Daily and provide the time at which you want the log cleanup to run in the Start field. Make sure that it’s Enabled and click OK.
Note: In this example, we have selected Daily. However, if you have scheduled RecoveryManager Plus to perform incremental backups weekly or monthly, you can change it accordingly.
Click on the Actions tab and click New.
In the Settings section,
Select PowerShell.exe in the Program/script field.
Enter -ExecutionPolicy Bypass C:\Scripts\CleanupLogs.ps1 in the Add arguments (optional): field.
Click OK.
Click OK.
The task has now been scheduled and your Exchange EWS logs will now be cleared at the specified time and frequency.
Click the created task CleanupLogs from the middle-pane and Click Run from the right-pane.
You can check the status of the last run operation in the Last Run Result column in the middle-pane.
Check if the CleanupLogs task cleared the logs in the following folder(s).
C:\Program Files\Microsoft\Exchange Server\V15\Logging\Ews\
Note: If you have added additional logs to be cleared using the script (line 10 of the script), check if those logs have also been successfully cleared.
Note: Please be aware that RecoveryManager Plus does not directly create any logs in your Exchange server nor does it delete existing logs. This article explains how you can clear logs that have been and will be created by your Exchange server.