Link to home
Start Free TrialLog in
Avatar of toddhd
toddhd

asked on

Programatically verify IIS is installed and running on XP

I am writing a install program that takes care of some of the tasks involved in installing and confguring an ASP.NET website. As part of the process, I'd like to very that IIS and SQL Server are installed and running. I have most of this process down - I wrote some functions that will take a Service Name as a parameter, and it will spit back whether the service exists, and is running. For example, to check is SQLServer is installed, I pass the value "MSSQLSERVER", as this is the service name that you see when you look in Admin Tools | Services.

What I don't seem to be able to find is which service relates directly to IIS. I do see a service called IIS Admin, and another called Word Wide Web Publishing, and I'm not sure if either of these is the exact IIS service? Maybe it is something else?

I guess this is an easy question, but I just can't figure out which one is correct. If you happen to know, or could point me to some documentation that explains it, I'd appreciate it.
Avatar of BurntSky
BurntSky

There are a couple ways you could go about doing it.  

One is to test for a registry key at "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\MajorVersion" and
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\MinorVersion" (the one for SQL Server is at "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion")

You could also check for the folder "%systemdir%\inetsrv"

You can also use Active Directory to test if IIS is exists and is running:

System.DirectoryServices.DirectoryEntry iisRoot = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root/");

There are probably other ways too.
most reliable ways that I know of would be these:


Dim rk As Microsoft.Win32.RegistryKey
Dim SQL As String
Dim IIS As String

rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion")
SQL = rk.GetValue("CurrentVersion")

rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\InetStp")
IIS = rk.GetValue("VersionString")

MessageBox.Show(SQL & ControlChars.NewLine & IIS)
if such values and keys don't exist in the registry then it means NOT INSTALLED and SQL and IIS values will be none! after that you can just check agains empty values with IF... ELSE... END IF
Avatar of toddhd

ASKER

davidlars99  - Thanks - that would likely tell me if it is installed, but would not indicate if it is running.
BurntSky - that probably looks right - I never thought of testing the IIS:// path, which would indeed probably indicate that IIS is at least running. Let me double check. I'm surprised that there appears to be way to directly check a service. I *think* I can just check if the IISAdmin service is running, as that is what starts and stops IIS, but I don't know if IIS can keep running without it. I'll have to stop it, and then see what happens.
in that case you have to dive into windows services and check it from there, copy and paste this code and see what you get



Imports System.ServiceProcess

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader4 As System.Windows.Forms.ColumnHeader
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader4 = New System.Windows.Forms.ColumnHeader
        Me.SuspendLayout()
        '
        'ListView1
        '
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4})
        Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListView1.FullRowSelect = True
        Me.ListView1.LabelEdit = True
        Me.ListView1.Location = New System.Drawing.Point(0, 0)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(400, 312)
        Me.ListView1.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.ListView1.TabIndex = 1
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader1
        '
        Me.ColumnHeader1.Text = "Display Name"
        Me.ColumnHeader1.Width = 200
        '
        'ColumnHeader2
        '
        Me.ColumnHeader2.Text = "Status"
        '
        'ColumnHeader3
        '
        Me.ColumnHeader3.Text = "Service Name"
        Me.ColumnHeader3.Width = 200
        '
        'ColumnHeader4
        '
        Me.ColumnHeader4.Text = "Type"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(550, 400)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim item As ListViewItem
        For Each srvc As ServiceController In ServiceController.GetServices
            item = ListView1.Items.Add(srvc.DisplayName)
            item.SubItems.Add(srvc.Status.ToString())
            item.SubItems.Add(srvc.ServiceName.ToString())
            item.SubItems.Add(srvc.ServiceType.ToString())
        Next
    End Sub

End Class
you would need to check "status" in the loop
Avatar of toddhd

ASKER

Thanks davidlars99 - If you check the original question, I already have a routine which will grab the service and check the status - the question is - what is the direct name of the IIS service? I can't seem to find it. I only seem to see services that support IIS, such as IIS Admin and World Wide Web Publishing - I don't think either of those is the actual IIS service itself.
you have to check against "srvc.ServiceName.ToString()" and I think IIS name is "IISADMIN" and SQL name is "MSSQLSERVER"

Dim IIS As Boolean=False
Dim SQL As Boolean=False

If srvc.ServiceName.ToString()="IISADMIN" Then
     IIS=IIF(srvc.ServiceName.Status.ToString()="Running", True, False)
End If

If srvc.ServiceName.ToString()="MSSQLSERVER" Then
     SQL=IIF(srvc.ServiceName.Status.ToString()="Running", True, False)
End If



sorry for the typo, in both cases it should be  >  "srvc.Status.ToString()" not "srvc.ServiceName.Status.ToString()"
ASKER CERTIFIED SOLUTION
Avatar of davidlars99
davidlars99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toddhd

ASKER

Thanks David - you gave me the most info, and I think I'll play around with those services to make sure which one is accurate
then all I have to say is good luck!  :)