question

HenryHong-7440 avatar image
0 Votes"
HenryHong-7440 asked YurongDai-MSFT answered

IIS Log Retention VBS / PowerShell Script to go through Multiple Folders

Good afternoon,

We have been looking into solutions to perform retention on IIS log to free up storage space.

We recently implemented a VBS script to run through task scheduler on a specific folder which did work successfully.

We now wanted to apply a solution for all our IIS log folders. Rather than creating a script and scheduled task for each folder, we wanted to look into a solution that could apply to a top-level folder and recursively filter down removing log files it finds in the directories below that are older than a specific age.

This is the current VBS script used for one particular folder:

 sLogFolder = "D:\inetpub\wwwroot\pcrlookup_com\Online_Reports\ReportPub\Reports\LogFiles"
 iMaxAge = 90   'in days
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 set colFolder = objFSO.GetFolder(sLogFolder)
    
         'Set objFolder = objFSO.GetFolder(colSubfolder.Path)
         Set colFiles = colFolder.Files
         For Each objFile in colFiles
    
                 if LCase(objFSO.GetExtensionName(objFile.Name)) = "xml" then
                         iFileAge = now-objFile.DateCreated
                         if iFileAge > (iMaxAge+1)  then
                                 objFSO.deletefile objFile, True
                 'wscript.echo objFile.Name
                         end if
                 end if
         Next


How would I convert this to search through multiple directories where it detects .log files?

Thanks.

windows-server-iis
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YurongDai-MSFT avatar image
0 Votes"
YurongDai-MSFT answered

Hi @HenryHong-7440,

You can try the following script:

Tips:
1. This script deletes IIS log files older than a specified number of days and runs it as a daily scheduled task on high-traffic web servers to avoid running out of disk space.
2. Edit the value of intDelAge to set the desired retention time on the server.
3. The location of the IIS log files is 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).
4. Save the code as a .vbs file and run it daily on the server as a scheduled job.

 Option Explicit
 WScript.Timeout = 82800
    
 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

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

Best regards,
Yurong Dai


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.