1

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?

techguy1029
  • 743
  • 10
  • 29
  • 1
    `"IIS://LOCALHOST"` is not an ActiveX component, as the error states. What are you trying to do with the `objIISOuter` object? – Étienne Laneville Nov 06 '19 at 19:04
  • 2
    You should switch to PowerShell. If you insist on VBScript, then IIS 6 compatibility must be enabled on your IIS installation, or it won't work. – Lex Li Nov 06 '19 at 19:05
  • @ÉtienneLaneville I'm trying to get the log files to zip them up. The edit should explain more what is going on – techguy1029 Nov 06 '19 at 19:08
  • @LexLi I think IIS 6 compatibility is enabled. And yeah, I would rather do this in powershell but if I can fix this script, that would be preferred – techguy1029 Nov 06 '19 at 19:58
  • In my opinion, you could directly use the “%SystemDrive%\inetpub\logs\LogFiles” path as the logs folder path and then you could use `Scripting.FileSystemObject` to delete the folder. More details you could refer to this [article](https://www.stevefenton.co.uk/2015/07/clean-out-old-iis-log-files/). – Brando Zhang Nov 07 '19 at 06:19

1 Answers1

1

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
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
  • Interesting, is there a reason the `IIS://LocalHost` path wouldn't work? And `%SystemDrive%\inetpub\logs\LogFiles` would? Shouldn't both be the same thing? – techguy1029 Nov 07 '19 at 22:30
  • 1
    I am not familiar with using `GetObject("IIS://LOCALHOST/W3SVC")` - what does this return? - so I suggested an approach I understand and should work as well. – Étienne Laneville Nov 07 '19 at 22:33