adOpenStatic Logo
Ken's Blog
Contact Ken
Copyright 2000 -

List/Enumerate all websites on a server, with their server state
Sample code (VBScript and VB.NET) to enumerate websites and their server state on IIS. Please note: the user context that is running this code must have permission to read the IIS metabase (so, typically it would need to be in the local Administrators group, or runnig as LocalSystem. Alternatively, you can use Metabase Explorer to add permissions for other users/groups to the appropriate metabase keys.

Option Explicit

Dim objWWW, Item
Dim aBinding, binding

With Response
   .Write("<table cellpadding=""5"" cellspacing=""0"">" & vbCrLf)
   .Write("<tr>" & vbCrLf)
   .Write("<th style=""text-align: left; border-bottom: 1px solid black;"">Site</th>" & vbCrLf)
   .Write("<th style=""text-align: left; border-bottom: 1px solid black;"">State</th>" & vbCrLf)
   .Write("</tr>" & vbCrLf)
End With

Set objWWW = GetObject("IIS://localhost/W3SVC")
For Each Item in ObjWWW

   If (Item.Class = "IIsWebServer") then
      Response.Write("<tr>" & vbCrLf)

      If (IsArray(aBinding)) then
         If aBinding(0) <> "" then
            binding = getBinding(aBinding(0))
         End If
         If aBinding <> "" then
            binding = getBinding(aBinding)
         End If
      End If

      Response.Write("<td><a href=""http://" & binding & """>" & item.ServerComment & "</a></td>" & vbCrLf)

      Response.Write("<td>" & GetState(Item.ServerState) & "</td>" & vbCrLf)


   End if

Response.Write("</table>" & vbCrLf)

Function getBinding(bindstr)

   Dim intPosFirstColon ' Int, Position of first colon
   Dim intPosSecondColon ' Int, Postition of second colon
   Dim intPort ' Int, Port site is listening on
   Dim strHostHeader ' String, HostHeader (if applicable, else use IP address)

   intFirstColon = InStr(bindstr, ":")
   intSecondColon = InStr(intFirstColon+1, bindstr, ":")

   intPort = Mid(bindstr, intFirstColon+1, intSecondColon-intFirstColon-1)
   strHostHeader = Mid(bindstr, intSecondColon+1)
   If strHostHeader = "" then strHostHeader = Request.ServerVariables("Local_Addr")
   getBinding=strHostHeader & ":" & intPort

End Function

Function GetState(intServerState)

   Select Case intServerState
   Case 1
      GetState = "Starting"
   Case 2
      GetState = "Started"
   Case 3
      GetState = "Stopping"
   Case 4
      GetState = "Stopped"
   Case 5
      GetState = "Pausing"
   Case 6
      GetState = "Paused"
   Case 7
      GetState = "Continuing"
   Case Else
      GetState = "Unknown"
   End Select

End Function

Comments on this FAQ are welcome

Back to FAQ Listing