Satchelmouth’s Weblog
Repository of solutions for things I encounter in my field of work

Script to list important details for all websites in IIS6

I have used David Wang’s original script and edited to include listing of physical path to website content and also write the results to a text file… Not written by me but my Developer, praise to be to him, saved me a ton of time fluffing about. I don’t know how elegant it is, but it definitely works.

Part of the script for listing home directory for website was taken from a sample script posted online, and then edited for my own use. I can’t remember exactly where it was from, so cannot give due credit 😦

 

OPTION EXPLICIT

DIM CRLF, TAB
DIM strServer
DIM objWebService
dim myFSO, WriteStuff, txtToWrite

dim objWMIService, colItemz, objItem, sPath
Set objWMIService = GetObject("winmgmts:{authenticationLevel=pktPrivacy}\\.\root\microsoftiisv2")

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

Set colItemz = objWMIService.ExecQuery ("Select * from IIsWebVirtualDirSetting where name = 'W3SVC/" & objWebserver.Name & "/root'")

For Each objItem in colItemz
sPath = objItem.Path
Next

WScript.Echo _
"Site ID = " & objWebserver.Name & CRLF & _
"Comment = """ & objWebServer.ServerComment & """ " & CRLF & _
"State = " & State2Desc( objWebserver.ServerState ) & CRLF & _
"LogDir = " & objWebServer.LogFileDirectory & CRLF & _
"Path = " & sPath & _
""

txtToWrite = txtToWrite & CRLF & "Site ID = " & objWebserver.Name & CRLF & _
"Comment = """ & objWebServer.ServerComment & """ " & CRLF & _
"State = " & State2Desc( objWebserver.ServerState ) & CRLF & _
"LogDir = " & objWebServer.LogFileDirectory & CRLF & _
"Path = " & sPath & _
""

' 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

txtToWrite = txtToWrite & CRLF & "IP Address" & TAB & _
"Port" & TAB & _
"Host" & CRLF & _
strBindings

END IF
END IF
NEXT

FileWriter txtToWrite

END SUB

sub FileWriter(WriteText)

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("OneOff.txt", 8, True)
WriteStuff.WriteLine(WriteText)
WriteStuff.Close

set WriteStuff = nothing
set myFSO = nothing

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 ) < 8 THEN strIP = strIP & TAB

EnumBindings = EnumBindings & _
strIP & TAB & _
strPort & TAB & _
strHost & TAB & _
""
NEXT

EnumBindings = EnumBindings & CRLF
NEXT

END FUNCTION

FUNCTION State2Desc( nState )
SELECT CASE nState
CASE 1
State2Desc = "Starting (MD_SERVER_STATE_STARTING)"
CASE 2
State2Desc = "Started (MD_SERVER_STATE_STARTED)"
CASE 3
State2Desc = "Stopping (MD_SERVER_STATE_STOPPING)"
CASE 4
State2Desc = "Stopped (MD_SERVER_STATE_STOPPED)"
CASE 5
State2Desc = "Pausing (MD_SERVER_STATE_PAUSING)"
CASE 6
State2Desc = "Paused (MD_SERVER_STATE_PAUSED)"
CASE 7
State2Desc = "Continuing (MD_SERVER_STATE_CONTINUING)"
CASE ELSE
State2Desc = "Unknown state"
END SELECT

END FUNCTION

Advertisements

2 Responses to “Script to list important details for all websites in IIS6”

  1. I like your code better than the original. More flexible 😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: