Search code examples
iisvbscriptruntime-erroriis-logs

VBscript for cleaning up IIS logs has runtime error


I have a VBScript for zipping up old IIS log files. I keep getting this error though:

Microsoft VBScript runtime error: ActiveX component can't create object: 'GetObject'

This is the line it errors on:

Set objIISOuter = GetObject("IIS://LOCALHOST")

I am unsure of what this means.

Tried what I found here and I wasn't able to get anything running with 32 or 64 bit.

I read somewhere that it could be a problem with a DLL not being registered but I don't know how this could be an issue here, might be wrong though.

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, _
          intZipAge, intDelAge)
      End If

I'm an admin so permissions aren't the issue. Any ideas?


Solution

  • Here are two potential approaches:

    Use the FileSystemObject to get the LogFiles folder and delete files:

    sLogFolder = "%SystemDrive%\inetpub\logs\LogFiles"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(sLogFolder)
    
    For Each objSubfolder In objFolder.SubFolders
        DeleteFiles objSubfolder.Path, 10
    Next
    

    Another approach:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objIIS = GetObject("winmgmts:root\WebAdministration")
    Set objSites = objIIS.InstancesOf("Site")
    
    For Each objSite In objSites
        DeleteFiles objSite.LogFile.Directory & "\w3svc\" & objSite.ID, 10
    Next
    

    Both approaches use the following Sub to delete the files from a folder:

    Sub DeleteFiles(p_sFolder, p_iMaxAge)
        Dim objFSO
        Dim objFolder
        Dim objFile
        Dim iFileAge
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(p_sFolder)
    
        If objFolder Is Nothing Then Exit Sub
    
        For Each objFile In objFolder.Files
            iFileAge = Now - objFile.DateCreated
            If iFileAge > (p_iMaxAge) Then
                objFSO.DeleteFile objFile, True
            End If
        Next
    
    End Sub