40FINGERS DNN Nederlands40FINGERS DNN English

Our Blog

Menu: Blog

Getting a list of all sites served by IIS

By: Stefan Kamphuis
Getting a list of all sites served by IIS

In order to prepare for an upcoming move of all sites hosted on our webservers to a new location, one of the tasks was to list which sites are served by IIS, including all hostheaders defined for those sites. The purpose of this list is to identify all domains we are handling.

Our "old" webserver is a Windows 2003 machine, and running IIS 6. Programming against IIS 6 is not my expertise, so i was kind of in the dark on this. Luckily, after a bit of googling around, I found this blogpost that explained how to enumerate website configuration of IIS 6. This post got me to about 95% of what I wanted: a list looking like this: 

SiteId Comment State LogDir RootDir IP Address Port Host
1 <Website Name> Started <Log Directory> <Website Root directory> All Unassigned 80 *


The blog post by David Wang uses ADSI to talk to IIS. You can find more info about this interface on MSDN. Anyhow, that post contained almost everything I needed, except for the Root Directory bit. Another googling round pointed me to this old (mid 2007) newsgroup thread (from microsoft.public.inetserver.iis), which contained this piece of code:

set objW3SVC = getObject("IIS://localhost/W3SVC")
for each IISwebSite in objW3SVC
    debug.print IISwebSite.ServerComment 'Web site name
    set objWeb = getObject(IISwebsite.adsPath & "/Root")
    rootPath = objWeb.Path

This was exactly the bit of information I was missing to finish the script. Running the script is very easy, just from the command prompt:

C:\Inetpub\AdminScripts>enumsites > enumsites.txt



This will create a tab separated text file containing all sites and all hostheaders etc. The file can be easily opened in Excel or Access for further processing.

This is the final version of the script:


DIM strServer
DIM objWebService

TAB  = CHR( 9 )
CRLF = CHR( 13 ) & CHR( 10 )

IF WScript.Arguments.Length = 1 THEN
    strServer = WScript.Arguments( 0 )
    strServer = "localhost"

WScript.Echo "Enumerating websites on " & strServer & CRLF
SET objWebService = GetObject( "IIS://" & strServer & "/W3SVC" )
EnumWebsites objWebService

SUB EnumWebsites( objWebService )
    DIM objWebServer, objWeb, strBindings, strBaseInfo

                WScript.Echo "SiteId" & TAB & _
                             "Comment" & TAB & _
                             "State" & TAB & _
                             "LogDir" & TAB & _
                             "RootDir" & TAB & _
                             "IP Address" & TAB & _
                             "Port" & TAB & _

    FOR EACH objWebServer IN objWebService
        IF objWebserver.Class = "IIsWebServer" THEN
            set objWeb = getObject(objWebServer.adsPath & "/Root")

            strBaseInfo = _
                objWebserver.Name & TAB & _
                objWebServer.ServerComment & TAB  & _
                State2Desc( objWebserver.ServerState ) & TAB & _
                objWebServer.LogFileDirectory & Tab & _
                objWeb.Path & Tab 

            ' Enumerate the HTTP bindings (ServerBindings) and
            ' SSL bindings (SecureBindings)
            EnumBindings objWebServer.ServerBindings, strBaseInfo
            EnumBindings objWebServer.SecureBindings, strBaseInfo

        END IF


SUB EnumBindings( objBindingList, strBaseInfo  )
    DIM i, strIP, strPort, strHost
    DIM reBinding, reMatch, reMatches
    SET reBinding = NEW RegExp
    reBinding.Pattern = "([^:]*):([^:]*):(.*)"

    FOR i = LBOUND( objBindingList ) TO UBOUND( objBindingList )
        ' objBindingList( i ) is a string looking like IP:Port:Host
        SET reMatches = reBinding.Execute( objBindingList( i ) )
        FOR EACH reMatch IN reMatches
            strIP = reMatch.SubMatches( 0 )
            strPort = reMatch.SubMatches( 1 )
            strHost = reMatch.SubMatches( 2 )

            ' Do some pretty processing
            IF strIP = "" THEN strIP = "All Unassigned"
            IF strHost = "" THEN strHost = "*"
            IF LEN( strIP ) < 8 THEN strIP = strIP & TAB

            WScript.Echo   strBaseInfo  & _
                           strIP & TAB & _
                           strPort & TAB & _



FUNCTION State2Desc( nState )
    SELECT CASE nState
    CASE 1
        State2Desc = "Starting"
    CASE 2
        State2Desc = "Started"
    CASE 3
        State2Desc = "Stopping"
    CASE 4
        State2Desc = "Stopped"
    CASE 5
        State2Desc = "Pausing"
    CASE 6
        State2Desc = "Paused"
    CASE 7
        State2Desc = "Continuing"
        State2Desc = "Unknown"



Categories: Windows Server

Related Articles

Installing DotNetNuke on Windows 2008R2 Windows 2008r2 is good in so many ways, I can never list them all. Of course, being the server variant of Windows 7, it means you can use all the nice...