Exchange CAS IIS Log Maintenance

One of the first things a young Exchange admin will learn is that there is no built in IIS log maintenance capability for clearing out old log files baked in to Exchange. Hopefully this lesson comes when SCOM starts throwing drive space alerts, and not when the C: drive fills up and craters the server :/. To mitigate this risk, most admins create a simple script for regularly performing IIS log maintenance by deleting CAS log files older than a certain date. Here are a couple of scripts you can use, depending on your environment.

Properly scheduled IIS log maintenance will help keep your C: drive clean (what was happening on 6/14!?)
15 items so the script is working as expected. Based on file size you may want to shorten the retention window to 7 days in your environment.

For info on how to create the Scheduled Task to run this script daily, look here

If you just have one CAS (or one server with the Client Access Service in the case of Exchange 2016), this one-liner will get the job done:

#################################################################
# Exchange CAS IIS Log Maintenance                              #
# This script deletes IIS log files older than 14 days from the #
# local Exchange CAS server					#
#								#
# Created by Eric Kukkuck 3/3/2015				#
#################################################################

Get-ChildItem –Path  “C:\inetpub\logs\LogFiles\W3SVC1” | Where-Object {$_.CreationTime –lt (Get-Date).AddDays(-14)} | Remove-Item

If you have multiple CASes and an Admin server, this script will pull a list of CAS servers from a text file and clear the old log files remotely. It will also remove log files from the first two additional log file directories, if you happen to have three directories in play (as I did at one point, the default website, a secondary website for basic auth ActiveSync devices, and a third website for the AV solution)

#################################################################
# Exchange CAS IIS Log Maintenance                              #
# This script deletes IIS log files older than 14 days from all #
# the Exchange CAS servers listed in the        		#
# D:\ABZ\Scripts\ExchangeCASServers.txt file			#
#								#
# Created by Eric Kukkuck 3/3/2015				#
#################################################################

$servers = get-content "D:\ABZ\Scripts\ExchangeCASServers.txt"

foreach ($server in $servers)
{
	Get-ChildItem –Path  “\\$server\c$\inetpub\logs\LogFiles\W3SVC1”,"\\$server\c$\inetpub\logs\LogFiles\W3SVC2","\\$server\c$\inetpub\logs\LogFiles\W3SVC3",“\\$server\c$\inetpub\logs\LogFiles\W3SVC4” | Where-Object {$_.CreationTime –lt (Get-Date).AddDays(-14)} | Remove-Item
}

-Eric

Leave a Reply

Your email address will not be published. Required fields are marked *