Skip to the main content.

6 min read

Automatic IIS log housekeeping

There is not much automation taking place in my current company.  I’m working to automate as many processes as possible, and therefore increase our productivity.  The article below was borrowed from the following link. http://www.808.dk/?code-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.

Save the code as a .vbs file and run it daily as a scheduled job on the server.

Note that the scripts using compression need the command line version of the free 7-Zip compressor available on http://www.7-zip.org/download.html

Housekeeping script, manual log locations, logs are archived and deleted:

Option ExplicitWScript.Timeout = 82800

‘ 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.

ArchiveLogFiles “D:Logfiles”, 30, 180ArchiveLogFiles “D:LogfilesW3SVC1”, 14, 30ArchiveLogFiles “D:LogfilesW3SVC243”, 5, 30ArchiveLogFiles “D:LogfilesSMTPSVC1”, 7, 60

Function ArchiveLogFiles(strLogPath, intZipAge, intDelAge)  Dim objFs  Dim objFsCheck  Dim objFolder  Dim objSubFolder  Dim objFile  Dim objWShell  Set objWShell = CreateObject(“WScript.Shell”)  Set objFs = CreateObject(“Scripting.FileSystemObject”)  Set objFsCheck = 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        ArchiveLogFiles strLogPath & objSubFolder.Name, intZipAge, 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) > intZipAge Then            objWShell.Run “7za.exe a -tzip “”” & strLogPath & _              Left(objFile.Name,Len(objFile.Name)-3) & “zip”” “”” & _              strLogPath & objFile.Name & “”””, 7, true            If objFsCheck.FileExists(strLogPath & _              Left(objFile.Name,Len(objFile.Name)-3) & “zip”) And _              (objFsCheck.FileExists(strLogPath & objFile.Name)) Then                objFsCheck.DeleteFile(strLogPath & objFile.Name)            End If          End If        ElseIf (InStr(objFile.Name, “ex”) > 0) _          And (Right(objFile.Name, 4) = “.zip”) Then          If DateDiff(“d”,objFile.DateLastModified,Date) > intDelAge Then            objFsCheck.DeleteFile(strLogPath & objFile.Name)          End If        End If      Next    Set objFs = Nothing    Set objFsCheck = Nothing    Set objFolder = Nothing    Set objWShell = nothing  End IfEnd Function

Housekeeping script, manual log locations, logs are just deleted:

Option ExplicitWScript.Timeout = 82800

‘ 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.

DeleteLogFiles “D:Logfiles”, 30DeleteLogFiles “D:LogfilesW3SVC1”, 14DeleteLogFiles “D:LogfilesW3SVC243”, 5DeleteLogFiles “D:LogfilesSMTPSVC1”, 7

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 IfEnd Function

Housekeeping script, automatic log locations, logs are archived and deleted:

Option ExplicitWScript.Timeout = 82800

‘ 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”).

Dim intZipAgeDim intDelAgeintZipAge = 30intDelAge = 180Dim objIISDim objWebDim objIISOuterDim 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 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 IfNext

Set objIIS = nothingSet objIISOuter = nothing

Function ArchiveLogFiles(strLogPath, intZipAge, intDelAge)  Dim objFs  Dim objFsCheck  Dim objFolder  Dim objSubFolder  Dim objFile  Dim objWShell  Set objWShell = CreateObject(“WScript.Shell”)  Set objFs = CreateObject(“Scripting.FileSystemObject”)  Set objFsCheck = 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        ArchiveLogFiles strLogPath & objSubFolder.Name, intZipAge, 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) > intZipAge Then            objWShell.Run “7za.exe a -tzip “”” & strLogPath & _              Left(objFile.Name,Len(objFile.Name)-3) & “zip”” “”” & _              strLogPath & objFile.Name & “”””, 7, true            If objFsCheck.FileExists(strLogPath & _              Left(objFile.Name,Len(objFile.Name)-3) & “zip”) And _              (objFsCheck.FileExists(strLogPath & objFile.Name)) Then                objFsCheck.DeleteFile(strLogPath & objFile.Name)            End If          End If        ElseIf (InStr(objFile.Name, “ex”) > 0) _          And (Right(objFile.Name, 4) = “.zip”) Then          If DateDiff(“d”,objFile.DateLastModified,Date) > intDelAge Then            objFsCheck.DeleteFile(strLogPath & objFile.Name)          End If        End If      Next    Set objFs = Nothing    Set objFsCheck = Nothing    Set objFolder = Nothing    Set objWShell = nothing  End IfEnd Function

Housekeeping script, automatic log locations, logs are just deleted:

Option ExplicitWScript.Timeout = 82800

‘ 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 intDelAgeintDelAge = 30Dim objIISDim objWebDim objIISOuterDim objWebOuterSet 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 IfNext

Set objIIS = nothingSet 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 IfEnd Function

Copilot for Sales vs Copilot for Service – What's the Difference?

Copilot for Sales vs Copilot for Service – What's the Difference?

The Copilot products just keep coming! Microsoft Copilot for Service and Copilot for Sales became generally available through the New Commerce...

Important 2024 Microsoft Licensing Updates

Important 2024 Microsoft Licensing Updates

There is some big news in the world of Microsoft licensing this month! In the summer of 2023, Microsoft modified the licensing for Microsoft 365,...

Transforming TCRG's Legacy Systems into a Secure Cloud Future with CloudServus

Transforming TCRG's Legacy Systems into a Secure Cloud Future with CloudServus

TCRG (The Consolidated Rehab Group), specializing in vocational rehabilitation for military personnel and veterans, partnered CloudServus, a leader...