A common request from my customers is a way to control the number of diagnostic log files Exchange 2013 creates. The number of parameters being logged in Exchange 2013 is far greater than in previous versions and therefor the space requirements are also greater. It’s not all that uncommon for the disks to fill up and if you are really unlucky, and you store the log files on the same disk as your transaction log files and/or your databases, you can make Exchange completely stop when running out of disk space. Not a good place to be in.
A quick google finds a couple of scripts dealing with this situation but I found some issues trying to make it all work so I thought I’d put together a post on how I made it work in my environments.
We need to do the following:
- Create the Purge-LogFiles.ps1 script from the TechNet Script Center repository
- Make one edit in the script
- Schedule the script in Task Scheduler
Seems easy enough but I found it to be a bit tricky. At least for my taste.
Step 1: Go to the link [https://gallery.technet.microsoft.com/scriptcenter/Purge-Exchange-Server-2013-c2e03e72] and copy the Powershell script block. (Do not download the file in the beginning of the page as it is an older version with some issues with it.) Open notepad and past the contents and save the file as Purge-LogFiles.ps1. Put it in a custom scripts folder (like D:\Custom Exchange Scripts\) on one of your Exchange 2013 Servers. I like to do it this way so I keep my custom scripts separated from the built in ones and I guess the Cumulative Updates recreates the default scripts folder and would remove my custom ones.
Step 2: Open the script file Purge-LogFiles.ps1 in Notepad and under ‘Edit’ > ‘Find’ enter *.log and hit ‘Find Next’. Directly after *.log type ,*.blg so that the line says -Include *.log,*.blg -Recurse. The reason for adding the *.blg is to let the script also remove DailyPerformanceLogs in $env:ExchangeInstallPath\Logging\Diagnostics\DailyPerformanceLogs folder. These files are rather big, accumulates over time and quickly fills you hard drives.
Step 3: Open Task Schedular (Run > Taskschd.msc) and choose ‘Create a Basic Task’. Add the following information:
- Create a Basic Task
Name: Delete Exchange Log Files
Description: Deletes Exchange Diagnostic LogFiles (*.log & *.blg) older than 14 days.
Daily > and in the next screen choose an appropriate start time and leave ‘Recur every:’ to 1 days.
Start a program
Program/script: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command “. ‘D:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto;. ‘D:\Custom Exchange Scripts\Purge-LogFiles.ps1’ -DaysToKeep 14 -Auto -SendMail -MailFrom firstname.lastname@example.org -MailTo email@example.com -MailServer localhost”(Obviously you need to alter the email addresses to your own. If you like me execute this script locally on your Exchange you can use ‘localhost’ as Mail Server.)
Hit ‘Yes’ when prompted
Check ‘Open the Properties dialog for this task when I click Finish’
On the ‘General’ tab, change to ‘Run whether user is logged on or not’ and if you are using User Account Control (UAC) you’ll also need to check ‘Run with highest privileges’
Also on the ‘General’ tab, select under which credentials you’d like the script to run as. Click ‘Finish’ and supply password.
To test if everything works ok, in Task Scheduler right-click the ‘Delete Exchange Log Files’ task and choose ‘Run’.
At last we have control over the amount of log files stored on our Exchange. Remember that the files in themselves are good, but just as long as they don’t break Exchange. 😉