A downloadable file for this article is available here.
The entire demonstration solution has been developed using Visual Studio 2005 Professional Edition on Windows Server 2003 Standard Edition. Please note that I didn’t really test the solution on any of the other versions/editions of the similar suite of Microsoft products.
You need to refer/import all of the following namespaces before you proceed further:
Imports System
Imports System.Management
Imports System.Data
“System.Management” has to be added separately to the list of references (which does not come by default).
The components of IIS 6.0
Retrieving native information from IIS 6.0 is not a tedious task. In fact, we should know about certain namespaces and classes, which are mainly used to retrieve information about IIS. Even though the title of this article says “ASP.NET 2.0,” the techniques discussed here should also work with “ASP.NET 1.1” (even though I didn’t test it). If you come across any problems, please do post so that I can reply to you.
Even though it is not essential, it is worthwhile to speak a little about IIS version 6.0 in this context. IIS 6.0 is the next generation of web server available in the Windows Server 2003 platform. IIS 6.0 obviously provides several enhancements over IIS 5.0 that are mainly intended to increase reliability, manageability, scalability, and security. IIS 6.0 is a key component of the Windows Server 2003 application platform, using which you can develop and deploy high performance ASP.NET Web applications, and XML Web Services.
IIS 6.0 internally contains a component called HTTP.SYS. HTTP.SYS is a kernel-mode (OS level) HTTP listener that listens for incoming requests and queues those requests up on the appropriate queue. Kernel mode support is not available in IIS 5.0. Each request will be serviced by one application pool. An application pool in IIS 6.0 can contain one or more applications.
There exists another component called Web Administration Service, which is a user-mode configuration and process manager. The Web Administration Service component performs mainly Configuration and Process Management. Application pools are used to manage a set of websites and applications. IIS 6.0 can support up to 2,000 application pools per server, and there can be multiple application pools operating at the same time.
The following sections will help you to retrieve IIS information using ASP.NET. You should realize that ASP.NET is not the only way to retrieve such information. You can also use VBScript, VB6, ISMAPI, WMI etc technologies to retrieve the same information. But our focus will be only on ASP.NET at the moment.
The following program helps you to retrieve basic IIS information.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = getPropertyStruct()
Dim dr As DataRow
Dim searcher As New ManagementObjectSearcher("root\MicrosoftIISv2", "SELECT * FROM IIsWebInfo")
For Each queryObj As ManagementObject In searcher.Get()
dr = dt.NewRow
dr("Name") = "Caption"
dr("value") = queryObj("Caption")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Description"
dr("value") = queryObj("Description")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "InstallDate"
dr("value") = queryObj("InstallDate")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "MajorIIsVersionNumber"
dr("value") = queryObj("MajorIIsVersionNumber")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "MinorIIsVersionNumber"
dr("value") = queryObj("MinorIIsVersionNumber")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Name"
dr("value") = queryObj("Name")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Status"
dr("value") = queryObj("Status")
dt.Rows.Add(dr)
Next
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
Catch err As ManagementException
Response.Write(err.Message)
End Try
End Sub
Details about the program
Even though the above program is a bit lengthy, it is very simple to understand. In the above program I am using “getPropertyStruct,” which is used to create and return a data table which can hold any number of “key,value” pairs. You can also work with “collections” instead of working with the “data table.” “getPropertyStruct” is defined as follows:
Private Function getPropertyStruct() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("Value"))
Return dt
End Function
The main statements within the above program (in the previous section) are as follows:
Dim searcher As New ManagementObjectSearcher("root\MicrosoftIISv2", "SELECT * FROM IIsWebInfo")
For Each queryObj As ManagementObject In searcher.Get()
The first statement connects to the “root\MicrosoftIISv2” namespace. We are trying to issue a SELECT statement based on a built-in class, “IIsWebInfo.” It would return all the objects associated with the class “IIsWebInfo” and I handle them (each) with “queryObj” which is of type “ManagementObject.”
With every object I receive from “queryObj,” I extract the information into a new “data row” which would finally be added to the “data table” (as follows).
dr = dt.NewRow
dr("Name") = "Name"
dr("value") = queryObj("Name")
dt.Rows.Add(dr)
Finally, I assign the data table information to the grid by issuing the following statements:
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
The next section further digs out the IIS information.
The modified code, to have a bit more information, would be as follows:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = getPropertyStruct()
Dim dr As DataRow
Dim searcher As New ManagementObjectSearcher("root\MicrosoftIISv2", "SELECT * FROM IIsWebService")
For Each queryObj As ManagementObject In searcher.Get()
dr = dt.NewRow
dr("Name") = "Name"
dr("value") = queryObj("Name")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "DisplayName"
dr("value") = queryObj("DisplayName")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "PathName"
dr("value") = queryObj("PathName")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Started"
dr("value") = queryObj("Started")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "State"
dr("value") = queryObj("State")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Status"
dr("value") = queryObj("Status")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "SystemName"
dr("value") = queryObj("SystemName")
dt.Rows.Add(dr)
Next
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
Catch err As ManagementException
Response.Write(err.Message)
End Try
End Sub
This time, instead of working with the “IIsWebInfo” class, I shifted to another class, “IIsWebService,” which gives you much more information than the previous class.
Retrieving all virtual directories present in IIS using ASP.NET 2.0
This is a bit different from the above coding style. First of all, let us look at the program.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = getVirtualRootStruct()
Dim dr As DataRow
Dim searcher As New ManagementObjectSearcher("root\MicrosoftIISv2", "SELECT * FROM IIsWebVirtualDir")
For Each queryObj As ManagementObject In searcher.Get()
dr = dt.NewRow
dr("AppRoot") = queryObj("AppRoot")
dr("Caption") = queryObj("Caption")
dr("Description") = queryObj("Description")
dr("Name") = queryObj("Name")
dr("Status") = queryObj("Status")
dt.Rows.Add(dr)
Next
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
Catch err As ManagementException
Response.Write(err.Message)
End Try
End Sub
To get the virtual directory information, we need to work with the class “IIsWebVirtualDir”. It has got a lot of properties. I just selected only the most important ones. I am using another routine to form a structure, to store that property information. The routine is as follows.
Private Function getVirtualRootStruct() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("AppRoot"))
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("Status"))
Return dt
End Function
All the columns are indirectly nothing but the properties, and the rest is the same as I explained in the first section.
To get further in-depth information about the virtual directories, we need to work with the class “IIsWebVirtualDirSettng.” It also has a lot of properties. I selected only the most important ones. I am using another routine to form a structure, to store that property information. The routine is as follows.
All the columns are indirectly nothing but the properties and the rest is the same as I explained in the first section.
How about retrieving SMTP, FTP, POP3 information using ASP.NET 2.0?
We can even retrieve that information as well. But we need to switch back again to the first style. Let us proceed with the following code.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = getPropertyStruct()
Dim dr As DataRow
Dim searcher As New ManagementObjectSearcher("root\MicrosoftIISv2", "SELECT * FROM IIsSmtpService")
For Each queryObj As ManagementObject In searcher.Get()
dr = dt.NewRow
dr("Name") = "DisplayName"
dr("value") = queryObj("DisplayName")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "PathName"
dr("value") = queryObj("PathName")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "Started"
dr("value") = queryObj("Started")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "State"
dr("value") = queryObj("State")
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Name") = "SystemName"
dr("value") = queryObj("SystemName")
dt.Rows.Add(dr)
Next
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
Catch err As ManagementException
Response.Write(err.Message)
End Try
End Sub
To retrieve information about SMTP, we need to work with the class “IIsSmtpService.” Similarly, to work with FTP, we need to work with “IIsFtpService,” and so on.
Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.