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
    Next
End Sub

Function SiteIsNotRunning( nState )

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

End Function

Function State2Desc( nState )

    Select Case nState
    Case 1
        'MD_SERVER_STATE_STARTING
        State2Desc = "Starting"
    Case 2
        'MD_SERVER_STATE_STARTED
        State2Desc = "Started"
    Case 3
        'MD_SERVER_STATE_STOPPING
        State2Desc = "Stopping"
    Case 4
        'MD_SERVER_STATE_STOPPED
        State2Desc = "Stopped"
    Case 5
        'MD_SERVER_STATE_PAUSING
        State2Desc = "Pausing"
    Case 6
        'MD_SERVER_STATE_PAUSED
        State2Desc = "Paused"
    Case 7
        'MD_SERVER_STATE_CONTINUING
        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



Tags