How can I be notified if a web site inside IIS is stopped?

Article ID: 209
Category: Scripts
Applies to: All Versions
Updated: 2019-07-25

You can launch the following VBScript through the application scheduler (e.g. every 1 minute) to be notified when a web site (or other component, e.g. SMTP) in IIS is stopped.

Note: The file needs to be saved with the .vbs extension and called it through cscript.exe.

' Lists the state of all IIS web sites configured on the local machine
' and returns an %ERRORLEVEL% of 1, if at least one web site is not in
' the "Started" state.
' When scheduling this script with EventSentry's application scheduler,
' make sure that the interpreter is set to "cscript.exe"

Option Explicit

Dim strServer, strServerType, strServerMetaType
Dim objService
Dim returnCode

returnCode          = 0
strServer           = "localhost"
strServerType       = "Web"
strServerMetaType   = "W3SVC"

Sub EnumServersites( objService )
    Dim objServer

    For Each objServer In objService
        If objServer.Class = "IIs" & strServerType & "Server" Then
                If SiteIsNotRunning(objServer.ServerState) Then
                    WScript.StdOut.Write "*"
                End If

            WScript.StdOut.Write _
                objServer.ServerComment & ": " & State2Desc( objServer.ServerState )

                If SiteIsNotRunning(objServer.ServerState) Then
                    WScript.StdOut.Write "*"
                    returnCode = 1
                End If

                WScript.StdOut.Write vbCRLF
        End If
End Sub

Function SiteIsNotRunning( nState )

    If nState <> 2 Then
        SiteIsNotRunning = 1
        SiteIsNotRunning = 0
    End If

End Function

Function State2Desc( nState )

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

End Function

SET objService = GetObject( "IIS://" & strServer & "/" & strServerMetaType )
EnumServersites objService

If returnCode <> 0 Then
    WScript.Echo vbCRLF & "WARNING: One or more IIS sites are not running" & vbCRLF
End If

WScript.Quit returnCode