Automatic IIS log housekeeping

A collection of scripts that can keep the IIS server's HTTP/SMTP/FTP logs from filling up drives.

Depending on the script chosen, you can have different archiving (compression) and file retention
times, or just simple delete-older-than log rotation. The log locations can be automatically retrieved
from the IIS metabase (additional configuration needed for IIS 7.x), or specified manually.

Update 2011-11-05: The functionality of script two and four have been further developed into a standalone
log rotation Windows service. If you want to avoid the hassle of setting
up a scheduled task for the log rotation, you can use the service instead.

' This Script archives (compresses to a zip file, then deletes the
' original) IIS log files older than a specified number of days.
' The script can also delete the compressed archive files older
' than another specified number of days.
'
' Run it as a daily scheduled task on high traffic web servers to
' avoid running out of disc space. IIS logs can typically be
' compressed to well below 1/20 of the original file size.
'
' The script needs the command line version of the free 7-Zip
' compressor available on http://www.7-zip.org/
'
' The ArchiveLogFiles function takes three parameters:
' "Path to log dir"
' "Compress log files older than n days and delete the original files"
' "Delete compressed log files older than n days"
'
' Multiple function calls can be added to archive files in different
' log folders with different log retentions.
'
' Note that the function runs through subfolders recursively, so if
' the same log retention should be used on a whole log folder tree
' structure, only one call with the root log folder is needed.
' Additional calls with specific subfolders can then be made to have
' shorter retentions on those.
'
' Edit the example lines below to match the log folder paths,
' archive and retention values needed on the server.

' This Script deletes IIS log files older than a specified number
' of days.
'
' Run it as a daily scheduled task on high traffic web servers to
' avoid running out of disc space.
'
' The DeleteLogFiles function takes two parameters:
' "Path to log dir"
' "Delete log files older than n days"
'
' Multiple function calls can be added to delete files in different
' log folders with different log retentions.
'
' Note that the function runs through subfolders recursively, so if
' the same log retention should be used on a whole log folder tree
' structure, only one call with the root log folder is needed.
' Additional calls with specific subfolders can then be made to have
' shorter retentions on those.
'
' Edit the example lines below to match the log folder paths and
' retention values needed on the server.

Function DeleteLogFiles(strLogPath, intDelAge)
Dim objFs
Dim objFolder
Dim objSubFolder
Dim objFile
Set objFs = CreateObject("Scripting.FileSystemObject")
If Right(strLogPath, 1) <> "\" Then
strLogPath = strLogPath & "\"
End If
If objFs.FolderExists(strLogPath) Then
Set objFolder = objFs.GetFolder(strLogPath)
For Each objSubFolder in objFolder.subFolders
DeleteLogFiles strLogPath & objSubFolder.Name, intDelAge
Next
For Each objFile in objFolder.Files
If (InStr(objFile.Name, "ex") > 0) _
And (Right(objFile.Name, 4) = ".log") Then
If DateDiff("d",objFile.DateLastModified,Date) > intDelAge Then
objFs.DeleteFile(strLogPath & objFile.Name)
End If
End If
Next
Set objFs = Nothing
Set objFolder = Nothing
End If
End Function

' This Script archives (compresses to a zip file, then deletes the
' original) IIS log files older than a specified number of days.
' The script can also delete the compressed archive files older
' than another specified number of days.
'
' Run it as a daily scheduled task on high traffic web servers to
' avoid running out of disc space. IIS logs can typically be
' compressed to well below 1/20 of the original file size.
'
' The script needs the command line version of the free 7-Zip
' compressor available on http://www.7-zip.org/
'
' Edit the values for intZipAge and intDelAge to set the archive and
' retention times needed on the server.
'
' The locations of the IIS log files are found automatically (for this
' to also work on IIS 7.x on Windows Vista, Windows Server 2008 or
' Windows 7, please enable "IIS 6 Metabase Compatibility" aka
' "IIS Metabase and IIS 6 configuration compatibility").

Set objIISOuter = GetObject("IIS://LOCALHOST")
For Each objWebOuter in objIISOuter
If LCase(objWebOuter.Class) = "iiswebservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iiswebserver" Then
Call ArchiveLogFiles( _
objWeb.LogFileDirectory & "\W3SVC" & objWeb.Name, _
intZipAge, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iissmtpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/SMTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iissmtpserver" Then
Call ArchiveLogFiles( _
objWeb.LogFileDirectory & "\SMTPSVC" & objWeb.Name, _
intZipAge, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iisftpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/MSFTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iisftpserver" Then
Call ArchiveLogFiles( _
objWeb.LogFileDirectory & "\MSFTPSVC" & objWeb.Name, _
intZipAge, _
intDelAge)
End If
Next
End If
Next

' This Script deletes IIS log files older than a specified number
' of days.
'
' Run it as a daily scheduled task on high traffic web servers to
' avoid running out of disc space.
'
' Edit the value for intDelAge to set retention times needed on
' the server.
'
' The locations of the IIS log files are found automatically (for this
' to also work on IIS 7.x on Windows Vista, Windows Server 2008 or
' Windows 7, please enable "IIS 6 Metabase Compatibility" aka
' "IIS Metabase and IIS 6 configuration compatibility").

Dim intDelAge
intDelAge = 30
Dim objIIS
Dim objWeb
Dim objIISOuter
Dim objWebOuter
Set objIISOuter = GetObject("IIS://LOCALHOST")
For Each objWebOuter in objIISOuter
If LCase(objWebOuter.Class) = "iiswebservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iiswebserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\W3SVC" & objWeb.Name, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iissmtpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/SMTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iissmtpserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\SMTPSVC" & objWeb.Name, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iisftpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/MSFTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iisftpserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\MSFTPSVC" & objWeb.Name, _
intDelAge)
End If
Next
End If
Next

Set objIIS = nothing
Set objIISOuter = nothing

Function DeleteLogFiles(strLogPath, intDelAge)
Dim objFs
Dim objFolder
Dim objSubFolder
Dim objFile
Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
Set objFs = CreateObject("Scripting.FileSystemObject")
If Right(strLogPath, 1) <> "\" Then
strLogPath = strLogPath & "\"
End If
If objFs.FolderExists(strLogPath) Then
Set objFolder = objFs.GetFolder(strLogPath)
For Each objSubFolder in objFolder.subFolders
DeleteLogFiles strLogPath & objSubFolder.Name, intDelAge
Next
For Each objFile in objFolder.Files
If (InStr(objFile.Name, "ex") > 0) _
And (Right(objFile.Name, 4) = ".log") Then
If DateDiff("d",objFile.DateLastModified,Date) > intDelAge Then
objFs.DeleteFile(strLogPath & objFile.Name)
End If
End If
Next
Set objFs = Nothing
Set objFolder = Nothing
Set objWShell = nothing
End If
End Function