Delete IIS Log Files Older Than 90 Days

Deletes IIS log files that are more than 90 days old.

 
 
 
 
 
4.3 Star
(9)
Add to favorites
Servers
8/10/2009
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • answer file
    1 Posts | Last post February 27, 2012
    • is there an easy way for this to look at a text file with server names?  Say i wanted to automate this for 20 servers by using an answer file
      
  • Script shouldn't prompt for hostname
    4 Posts | Last post October 19, 2011
    • Please provide script which we can configure in scheduled job in Windows 2008 R2 or SP1 server which should not prompt for server hostname,
      Thanks
    • This should work.  Just finished modifying the script to do just that.
      
      MaxDays = 90
      
      strComputer = "LocalHost"
      Count = 0
      wmiQuery = "Select * from Win32_OperatingSystem"
      
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
      Set colItems = objWMIService.ExecQuery(wmiQuery) 
          For Each objItem In colItems 
              sWindowsPath = objItem.WindowsDirectory 
          Next 
      Set objW3SVC = GetObject("IIS://" & strComputer & "/W3SVC")
          For Each objSite In objW3SVC 
              If objSite.Class = "IIsWebServer" Then 
                  strLogDir = UCase(objSite.LogFileDirectory) 
                      strLogDir = Replace(strLogDir,"%WINDIR%",sWindowsPath,1,1,1) 
                      strLogDir = Replace(strLogDir,"%SYSTEMROOT%",sWindowsPath,1,1,1) 
                      strLogDir = Replace(strLogDir,":","$",1,1,1) 
                  objLogFolder = "\\" & strComputer & "\" & strLogDir 
                  Set oFSO = CreateObject("Scripting.FileSystemObject") 
                  Set oFolder = oFSO.GetFolder(objLogFolder) 
                  Set colSubFolders = oFolder.Subfolders 
                  For Each oSubFolder In colSubFolders 
                      If InStr(UCase(oSubFolder),"W3SVC") Then 
                          For Each oFile In oSubFolder.files 
                              If InStr(LCase(oFile.Name),".log") Then 
                                   If (Date - oFile.DateCreated > CInt(MaxDays)) Then 
                                      oFSO.DeleteFile(oSubFolder & "\" & oFile.Name)
                                  End If 
                              End If 
                          Next 
                      End If 
                  Next 
              End If 
          Next 
      WScript.Echo "Finished"
      
    • Let's try posting that again...
      
      MaxDays = 90
      
      strComputer = "LocalHost"
      wmiQuery = "Select * from Win32_OperatingSystem"
      
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery(wmiQuery)
          For Each objItem In colItems
              sWindowsPath = objItem.WindowsDirectory
          Next
      Set objW3SVC = GetObject("IIS://" & strComputer & "/W3SVC")
          For Each objSite In objW3SVC
              If objSite.Class = "IIsWebServer" Then
                  strLogDir = UCase(objSite.LogFileDirectory)
                      strLogDir = Replace(strLogDir,"%WINDIR%",sWindowsPath,1,1,1)
                      strLogDir = Replace(strLogDir,"%SYSTEMROOT%",sWindowsPath,1,1,1)
                      strLogDir = Replace(strLogDir,":","$",1,1,1)
                  objLogFolder = "\\" & strComputer & "\" & strLogDir
                  Set oFSO = CreateObject("Scripting.FileSystemObject")
                  Set oFolder = oFSO.GetFolder(objLogFolder)
                  Set colSubFolders = oFolder.Subfolders
                  For Each oSubFolder In colSubFolders
                      If InStr(UCase(oSubFolder),"W3SVC") Then
                          For Each oFile In oSubFolder.files
                              If InStr(LCase(oFile.Name),".log") Then
                                   If (Date - oFile.DateCreated > CInt(MaxDays)) Then
                                      oFSO.DeleteFile(oSubFolder & "\" & oFile.Name)
                                  End If
                              End If
                          Next
                      End If
                  Next
              End If
          Next
      WScript.Echo "Finished"
      
    • Sorry I tried.  Website isn't keeping the script formatting.  :(
      
      Anyway, all you have to do is remove the line for the msgbox that prompts for computer name and instead add a line strComputerName = "LocalHost".  Then you can remove the end msgbox ("Done") as well.