Howto: Print Site IDs to File in IIS

August 24, 2010

First create a file called c:\listsites.vbs with the following content:

OPTION EXPLICIT                                                                 
                                                                                
DIM CRLF, TAB                                                                   
DIM strServer                                                                   
DIM objWebService                                                               
                                                                                
TAB  = CHR( 9 )                                                                 
CRLF = CHR( 13 ) & CHR( 10 )                                                    
                                                                                
IF WScript.Arguments.Length = 1 THEN                                            
    strServer = WScript.Arguments( 0 )                                          
ELSE                                                                            
    strServer = "localhost"                                                     
END IF                                                                          
                                                                                
WScript.Echo "Enumerating websites on " & strServer & CRLF                      
SET objWebService = GetObject( "IIS://" & strServer & "/W3SVC" )                
EnumWebsites objWebService                                                      
                                                                                
                                                                                
SUB EnumWebsites( objWebService )                                               
    DIM objWebServer, strBindings                                               
                                                                                
    FOR EACH objWebServer IN objWebService                                      
        IF objWebserver.Class = "IIsWebServer" THEN                             
            WScript.Echo _                                                      
                "Site ID = " & objWebserver.Name & CRLF & _                     
                "Comment = """ & objWebServer.ServerComment & """ " & CRLF & _  
                "State   = " & State2Desc( objWebserver.ServerState ) & CRLF & _
                "LogDir  = " & objWebServer.LogFileDirectory & _                
                ""                                                              
                                                                                
            ' Enumerate the HTTP bindings (ServerBindings) and                  
            ' SSL bindings (SecureBindings)                                     
            strBindings = EnumBindings( objWebServer.ServerBindings ) & _       
                          EnumBindings( objWebServer.SecureBindings )           
            IF NOT strBindings = "" THEN                                        
                WScript.Echo "IP Address" & TAB & _                             
                             "Port" & TAB & _                                   
                             "Host" & CRLF & _                                  
                             strBindings                                        
            END IF                                                              
        END IF                                                                  
    NEXT                                                                        
                                                                                
END SUB                                                                         
                                                                                
FUNCTION EnumBindings( objBindingList )                                         
    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 ) 

Create another file called listsites.bat (anywhere) with this content:

cscript c:\listsites.vbs > c:\listsites.txt
C:\WINDOWS\NOTEPAD.EXE c:\listsites.txt