reg88.com

fine...I'll blog too

Keep IIS Logs Clean and Keep Only a Week Worth of Logs

Webserver logs can add up and eventually fill up your entire disk if not mananged properly. Windows and IIS don’t exactly give you flexible options to manage logs either. So what to do? Here’s simple solution. We’re going to create couple of scripts and kick off a Task to purge old logs.

Lets create a folder to store the scripts

1
mkdir C:scripts

Create a batch file to kick off a powershell script

1
2
notepad C:scriptsDelete-Old-IIS-Logs.bat #Add the below line to the .bat file
powershell.exe C:scriptsDelete-Old-IIS-Logs.ps1

Lets create a powershell script that will clean up the logs and leave 7 days worth

1
2
3
4
5
6
7
notepad C:scriptsDelete-Old-IIS-Logs.ps1 #Add the below lines to the .ps1 file
$Path = “C:inetpublogsLogFilesW3SVC1″
$LogsToKeep = “7″
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays(-$LogsToKeep)

Get-ChildItem $Path -recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

Create a new Task via Task Scheduler and Check the following settings:
– Run whether user is logged on or not
– Run with highest privileges
– Action should be: Start a Program and pointing to “:scriptsDelete-Old-IIS-Logs.bat”