The Exchange Performance Logs Script cleans up logs missed by backups!

Introduction

Exchange 2013 isn’t exactly new at this point, but with every cumulative update (CU) Microsoft seems to find more things to change. Since the release of Exchange 2013 there are 3 log files that have caused some issues for me in my professional life. So much so I had to write The Daily Exchange Performance logs Script to solve the issues. The OWA logs, ECP logs, and the Daily Exchange Performance Logs are the biggest annoyances because they are not flushed out with traditional Exchange backup software.

Update

This script has been moved and updated under the TDSheridanLab Github Account available here!
TDSheridan Lab Github

Daily Exchange Performance Logs

For those who don’t know The Exchange Daily Performance Logs are a scheduled task that runs certain performance counts in performance monitor. These daily logs are between 600mb and 1 gig a day for a small company and grow based on server usage. If you disable the scheduled task, but then at 3am The Microsoft Exchange Diagnostics Service will crash and then automatically restart. If the task is disabled, then the Diagnostic Service can’t interact with it and crashes.

I know that doesn’t sound like a big deal overall, but if you have a network monitoring software it could generate an alert… Every day. I’ll take this a step further; Professionally I work for an MSP so if I were to disable the schedule task then I would have alerts triggering every day for all of our clients that run Exchange 2013. Needless to say, I came up with a better solution.

Why do we need a Script?

The Daily Performance Logs, OWA Logs, and ECP Logs are not flushed out by backup software either. The simplest solution is to create a scheduled task to run a script to automatically delete the log files. To do this I decided to use PowerShell scripting to accomplish this task.

In the following script is set to delete the log files that are older than 3 days and the script is configured to use the default Exchange/IIS locations. If you changed those locations, you’ll have to update the script.

#----- Daily Performance Logs-----#
#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "3"
#----- define folder where files are located ----#
$TargetFolder = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs"
#----- define extension ----#
$Extension = "*.blg"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
Remove-Item $Files
#----- W3SVC1 Folder-----#
#----- define folder where files are located ----#
$TargetFolder1 = "C:\inetpub\logs\LogFiles\W3SVC1"
#----- define extension ----#
$Extension1 = "*.log"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
$Files1 = Get-Childitem $TargetFolder1 -Include $Extension1 -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
Remove-Item $Files1
#----- W3SVC2 Folder-----#
#----- define folder where files are located ----#
$TargetFolder2 = "C:\inetpub\logs\LogFiles\W3SVC2"
#----- define extension ----#
$Extension2 = "*.log"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
$Files2 = Get-Childitem $TargetFolder2 -Include $Extension2 -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
Remove-Item $Files2

I configured the script for to delete items older then 3 days just in case there was a server issue. The logs would still be there to be able to fix it. So far, I haven’t looked at those logs once, but that doesn’t mean I’ll never have to do it.

Conclusion

Then to finish this out create a scheduled task and let it run whenever works best for you. Remember that the account that you use to run the daily Exchange Performance logs script will need the log on as a service active directory right.