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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
' 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