Here is sample code to return a collection of Application Pools using WMI and IIS7. This is dynamic because this could be used to retrieve from a local machine or altering the code to connect to a remote server running IIS7. Here is an article that explains what IIS7 features are on various versions of Vista. This is bound to confuse! 🙂 http://www.iis.net/1100/SinglePageArticle.ashx
'Using System.Management to retrieve WMI / IIS7 info.
'Define the WMI connection information
Dim options As New System.Management.ConnectionOptions()
'options.Username = strUID
'options.Password = strPWD
'Define the Scope information / Note the path defined.
Dim scope As System.Management.ManagementScope
scope = New System.Management.ManagementScope(\.rootWebAdministration)
'Define Query and Searcher objects
Dim WMIQuery As New System.Management.SelectQuery("SELECT * FROM ApplicationPool")
Dim searcher As New System.Management.ManagementObjectSearcher(scope, WMIQuery)
'Connect to WMI
Try
scope.Connect()
Catch ex As Exception
Exit Sub
End Try
'Dim variables for information that will be returned
Dim AppPoolName As System.Management.ManagementObject
Dim col As System.Management.ManagementObjectCollection
'Return the collection
col = searcher.Get()
'Write the list of Application Pools to webpage
For Each AppPoolName In col
Response.Write(AppPoolName.GetPropertyValue("Name").ToString() & "<BR>")
Next
Here is the IIS7'ish way of using the Microsoft.Web.Administration
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As ApplicationPoolCollection
col = Server.ApplicationPools
Dim AppPoolName As String = ""
Dim x As Integer = 0
For x = 0 To col.Count – 1
AppPoolName = col(x).Name
Next
'Return a list of Sites
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As SiteCollection
col = Server.Sites
Dim SiteName As String = ""
Dim x As Integer = 0
For x = 0 To col.Count – 1
SiteName = col(x).Name
Next