For this first post, I am going to focus on what to do about those Exchange IIS logs. Going into the new environment, I (mistakenly) assumed that the logging happening in C:\inetpub\logs\LogFiles\W3SVC* directories (or your organization's equivalent) would be getting handled better or maybe be more automatic, or just anything. In our former 2010 environment, it was a regular cleanup task for us - we had relatively smaller OS disks, and we'd be getting disk space alerts at least once a month. For our 2013 environment, I made sure to dedicate more disk space to the OS drive, but I finally got my first disk space alert and traced it back to these logs.
My initial reaction to this development was to begin researching what these logs are, whether or not they are useful, and whether or not they should be kept. Based on the articles I found, it seems others share my annoyance with these logs and most of them just flat out eliminate them after a period ranging from a couple of days to a couple of months. They also link to or provide scripts that seem overly complicated - as you've seen from earlier examples of my Exchange Management Shell scripts, I'm a fan of keeping things simple and understandable. (Refer to articles here, here, here, and here with input from other admins regarding actions to take and scripts to make.)
Our practice had been to zip the logs from the previous month and to store them for a couple months until we needed to reclaim some space. While there's plenty of argument out there for just deleting the logs, my luck is that I'm going to need them for something after they're gone, so I wanted to continue our practice of keeping things around for a bit. My goals with a script to deal with these logs were pretty simple:
- Script is to be run automatically via Task Scheduler on 15th of every month
- Using the current date, gather the numeric values of the year and the month from 30 days previous
- Use this value to zip log files corresponding to the previous month via 7-zip
- Note that our log names are in the format of "u_exYYMMDD.log"
- Delete the archived log files when done
On to building the script! First, we need to do some date manipulation. It all begins with the Get-Date cmdlet (here and here). Since I want last month's values, I'll do the following:
$prevMonth = (Get-Date).AddDays(-30)
This gives too much information - we want to locate the logs via "*YYMM*.log" - so now we need to pare it down to the two-digit values of the year and the month. After some more digging on how to get "Get-Date" to do this, it turns out you can use a parameter called "Uformat" to get pieces of the date:
$filterDate = Get-Date $prevMonth -UFormat %y%m
Now we take the "$filterDate" and use it to filter for the correct logs (this was referenced in the paragraph above):
$filter = "*$filterDate*.log"
We're going to need a name for our zip file, so let's keep working with the date. We use the format of YYYYMM.zip for our archived log files, so let's get that information out of the date:
$zipYear = (Get-Date $prevMonth).Year $zipMonth = Get-Date $prevMonth -UFormat %m $zipName = "$zipYear" + "$zipMonth" + ".zip"
Time to zip the files! As a reminder, I'm using 7-zip for this process, so you may need to update my code to reflect the proper command format for your archiving software of choice. The format for 7-zip is to call the CLI version of the executable, give it the switch to archive, the name of the zip file, and what to archive:
& 'E:\Program Files\7-Zip\7z.exe' a C:\inetpub\logs\LogFiles\W3SVC1\$zipName C:\inetpub\logs\LogFiles\W3SVC1\$filter
Once the zip process has been completed, we need to delete the log files:
I was able to test all of this out (after backing up the logs!) and confirmed things are working the way I want them to. The final step for me was to configure the script to run via Task Scheduler, so now it's just a matter of waiting for next month to reap the benefit of this script.
Thank you for stopping by, and I hope the above has been useful - credit/link for the script is appreciated if so! As always, I welcome any feedback - I'm open to ways to improve the script or alternate/better ways of doing things.
Remove-Item C:\inetpub\logs\LogFiles\W3SVC1\$filter
I was able to test all of this out (after backing up the logs!) and confirmed things are working the way I want them to. The final step for me was to configure the script to run via Task Scheduler, so now it's just a matter of waiting for next month to reap the benefit of this script.
Thank you for stopping by, and I hope the above has been useful - credit/link for the script is appreciated if so! As always, I welcome any feedback - I'm open to ways to improve the script or alternate/better ways of doing things.