Automatically delete log files in Exchange 2013

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:

  1. Create the Purge-LogFiles.ps1 script from the TechNet Script Center repository
  2. Make one edit in the script
  3. 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.
  • Trigger
    Daily > and in the next screen choose an appropriate start time and leave ‘Recur every:’ to 1 days.
  • Action
    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 user@domain.com -MailTo user@domain.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
  • Finish
    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. 😉

 

 

Advertisements
This entry was posted in Exchange - On-Premises and tagged , . Bookmark the permalink.

2 Responses to Automatically delete log files in Exchange 2013

  1. Jones says:

    I set this up and unfortunately the task runs but apparently the script does not. The script runs just fine when run manually from within the EMS but when triggering from task scheduler it is not working.

    Like

  2. thejesperbernle says:

    Hi Jones,

    Please double check the string under “Action”. I’ve experienced the same issue and if I remember correctly I’d missed something in the “Action” string.

    BR / Jesper

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s