Monitoring Services in Windows Servers via VBScript


Here is a script I wrote to monitor services in Windows Server which became very useful for my job.

If you want to use this script, don’t forget the following things.

1. You need to supply a server name to this script as a parameter
2. You need to supply values for the following variables in the script manually
        – sFrom : From E-email address, could be a fake address
        – sTo : who wants to receive alerts emails when the services isn’t running. Multiple email addresses can be entered separated by semi-colon (NOT COMMA, remember this is Microsoft).
        – sSMTPServerName : A local SMTP server name

I wrote a batch file where I call this script with different server names like below,

CScript C:ScriptServiceMon.vbs ServerName1
CScript C:ScriptServiceMon.vbs ServerName2

You can copy and paste the script as below OR download it from HERE.

‘******************************************************************************
‘ Name: ServiceMon.vbs
‘ Purpose: This script will check all the services on the specified server and send e-mail
‘ to specified e-mail addresses when services (set to Automatic start) is not running state
‘ You should be able to invoke this script via batch file and run it intervals to monitor the
‘ services on multple servers
‘ Parameter: ServerName
‘ E.g., CScript ServiceMon.vbs MySQLServer
‘ Written by: Anand Venkatachalapaty
‘******************************************************************************
‘On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim MonitoredServer
Dim arrComputers
Dim StrComputer
Dim objItem, objWMIService, colItems
Dim sSubject, sMailText
Dim sFrom, sTo
Dim sSMTPServerName

MonitoredServer = WScript.Arguments(0)

‘NOTE HERE…NOTE HERE…NOTE HERE
‘Type the From and To Addresses here and SMTP server name
sFrom = MoniteredServer & “@mycompany.com”    ‘if you want, you can type any fake address here
sTo = “myself@company.com; sysadmins@company.com” ‘multiple address sepearted by semi-colon
sSMTPServerName = “mail.company.com”

arrComputers = Array(MonitoredServer)
For Each strComputer In arrComputers

   Set objWMIService = GetObject(“winmgmts:\” & _
        strComputer & “rootCIMV2”)
   Set colItems = objWMIService.ExecQuery(“SELECT * FROM _
        Win32_Service”, “WQL”, _
        wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems

      If StrComp(objItem.State,”Running”) <> 0 Then
        If StrComp(objItem.StartMode,”Auto”) = 0 Then
            If StrComp(objItem.DisplayName,”Performance Logs and Alerts”) = 0 Then
                ‘Skip it
            Else
                sSubject = UCase(MonitoredServer) & “: ” & objItem.DisplayName  & ” Service is ” & UCase(objItem.State)
                sMailText = “Stopped Service Details on Server ” & UCase(MonitoredServer) &  vbCrLf
                sMailText = sMailText & “Service Name: ” & objItem.DisplayName & vbCrLf
                sMailText = sMailText & “Started: ” & objItem.Started & vbCrLf
                sMailText = sMailText & “StartMode: ” & objItem.StartMode & vbCrLf
                sMailText = sMailText & “State: ” & objItem.State & vbCrLf
                sMailText = sMailText & “Status: ” & objItem.Status & vbCrLf
                sMailText = sMailText & “ExitCode: ” & objItem.ExitCode & vbCrLf
                sMailText = sMailText & “PathName: ” & objItem.PathName & vbCrLf
                sMailText = sMailText & “ServiceSpecificExitCode: ” & objItem.ServiceSpecificExitCode &vbCrLf
                InformAdmins sFrom,sTo,sSubject,sMailText,2
            End If
        End If
      End If
   Next
Next

Function WMIDateStringToDate(dtmDate)
    WScript.Echo dtm:
        WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & “/” & _
        Mid(dtmDate, 7, 2) & “/” & Left(dtmDate, 4) _
        & ” ” & Mid (dtmDate, 9, 2) & “:” & Mid(dtmDate, 11, 2) & “:” & Mid(dtmDate,13, 2))
End Function

‘*******************************************************************************
‘* Function Name: InformAdmins
‘* Purpose: Send mail using CDO.Message object which works in Windows 2000/2003
‘*          Servers.
‘*
‘* Sub Routine Arguments:
‘*   “From address”,”To address”,”Subject”, “Body Text”,”Prority”
‘*
‘* Importance argument should be 0 or 1 or 2.
‘* 0 – Low Priority
‘* 1 – Normal
‘* 2 – High Priority
‘*
‘* Written Date: 5/7/04
‘* Written By: Anand Venkatachalapathy
‘*******************************************************************************

Sub InformAdmins(sFrom,sTo,sSubject,sBody,nPriority)
    DIM iMsg, Flds, iConf

    ‘* Assign cdoSendUsingPort is set to 2, i.e., send using SMTP (25) port
    Const cdoSendUsingPort = 2

    ‘* Create CDO Objects and assign to variables
    Set iMsg = CreateObject(“CDO.Message”)
    Set iConf = CreateObject(“CDO.Configuration”)
    Set Flds = iConf.Fields

    ‘* Assign values to Flds class properties
    With Flds
    .Item(“
http://schemas.microsoft.com/cdo/configuration/sendusing”) _
                                     = cdoSendUsingPort
    .Item(“
http://schemas.microsoft.com/cdo/configuration/smtpserver”) _
                                     = sSMTPServerName
    .Item(“
http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”)_
                                 = 25 
    .Update
    End With

    ‘* Assign message properties and Send the mail
    With iMsg
       Set .Configuration = iConf
       .Fields(“urn:schemas:httpmail:importance”).Value = nPriority
       .Fields.Update
       .To = sTo
       .From = sFrom
       .Sender = sFrom
       .Subject = sSubject
       .TextBody = sBody
       .Send
    End With
End Sub
‘*******************************************************************************
‘* End of Script
‘*******************************************************************************

As per request from Bhaskar, here the modified script to send alerts via one email for all servers. Call the script like this “CScript Servicemon.vbs Server1 server2 server3 server4” like all of your servers as parameters delimited by space.

Here is the modified script:

‘******************************************************************************
‘ Name: ServiceMon.vbs
‘ Purpose: This script will check all the services on the specified server and send e-mail
‘ to specified e-mail addresses when services (set to Automatic start) is not running state
‘ You should be able to invoke this script via batch file and run it intervals to monitor the
‘ services on multple servers
‘ Parameter: ServerName
‘ E.g., CScript ServiceMon.vbs MySQLServer
‘ Written by: Anand Venkatachalapaty
‘******************************************************************************
‘On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim MonitoredServer
Dim arrComputers
Dim StrComputer
Dim objItem, objWMIService, colItems
Dim sSubject, sMailText
Dim sFrom, sTo
Dim sSMTPServerName

MonitoredServer = WScript.Arguments(0)

‘NOTE HERE…NOTE HERE…NOTE HERE
‘Type the From and To Addresses here and SMTP server name
sFrom = MoniteredServer & “@mycompany.com”    ‘if you want, you can type any fake address here
sTo = “myself@company.com; sysadmins@company.com” ‘multiple address sepearted by semi-colon
sSMTPServerName = “mail.company.com”
sSubject = “Service Monitor – and type your subject text here”

arrComputers = Array(MonitoredServer)
For Each strComputer In arrComputers

   Set objWMIService = GetObject(“winmgmts:\” & _
        strComputer & “rootCIMV2”)
   Set colItems = objWMIService.ExecQuery(“SELECT * FROM _
        Win32_Service”, “WQL”, _
        wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems

      If StrComp(objItem.State,”Running”) <> 0 Then
        If StrComp(objItem.StartMode,”Auto”) = 0 Then
            If StrComp(objItem.DisplayName,”Performance Logs and Alerts”) = 0 Then
                ‘Skip it
            Else
                sMailText = ” * * * * * ” & UCase(MonitoredServer) & ” * * * * * ” & vbCrLf
                sMailText = “Stopped Service Details on Server ” & UCase(MonitoredServer) &  vbCrLf
                sMailText = sMailText & “Service Name: ” & objItem.DisplayName & vbCrLf
                sMailText = sMailText & “Started: ” & objItem.Started & vbCrLf
                sMailText = sMailText & “StartMode: ” & objItem.StartMode & vbCrLf
                sMailText = sMailText & “State: ” & objItem.State & vbCrLf
                sMailText = sMailText & “Status: ” & objItem.Status & vbCrLf
                sMailText = sMailText & “ExitCode: ” & objItem.ExitCode & vbCrLf
                sMailText = sMailText & “PathName: ” & objItem.PathName & vbCrLf
                sMailText = sMailText & “ServiceSpecificExitCode: ” & objItem.ServiceSpecificExitCode &vbCrLf & vbCrLf
            End If
        End If
      End If
   Next
Next

‘Send the email
InformAdmins sFrom,sTo,sSubject,sMailText,2

Function WMIDateStringToDate(dtmDate)
    WScript.Echo dtm:
        WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & “/” & _
        Mid(dtmDate, 7, 2) & “/” & Left(dtmDate, 4) _
        & ” ” & Mid (dtmDate, 9, 2) & “:” & Mid(dtmDate, 11, 2) & “:” & Mid(dtmDate,13, 2))
End Function

‘*******************************************************************************
‘* Function Name: InformAdmins
‘* Purpose: Send mail using CDO.Message object which works in Windows 2000/2003
‘*          Servers.
‘*
‘* Sub Routine Arguments:
‘*   “From address”,”To address”,”Subject”, “Body Text”,”Prority”
‘*
‘* Importance argument should be 0 or 1 or 2.
‘* 0 – Low Priority
‘* 1 – Normal
‘* 2 – High Priority
‘*
‘* Written Date: 5/7/04
‘* Written By: Anand Venkatachalapathy
‘*******************************************************************************

Sub InformAdmins(sFrom,sTo,sSubject,sBody,nPriority)
    DIM iMsg, Flds, iConf

    ‘* Assign cdoSendUsingPort is set to 2, i.e., send using SMTP (25) port
    Const cdoSendUsingPort = 2

    ‘* Create CDO Objects and assign to variables
    Set iMsg = CreateObject(“CDO.Message”)
    Set iConf = CreateObject(“CDO.Configuration”)
    Set Flds = iConf.Fields

    ‘* Assign values to Flds class properties
    With Flds
    .Item(“http://schemas.microsoft.com/cdo/configuration/sendusing“) _
                                     = cdoSendUsingPort
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver“) _
                                     = sSMTPServerName
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout“)_
                                 = 25 
    .Update
    End With

    ‘* Assign message properties and Send the mail
    With iMsg
       Set .Configuration = iConf
       .Fields(“urn:schemas:httpmail:importance”).Value = nPriority
       .Fields.Update
       .To = sTo
       .From = sFrom
       .Sender = sFrom
       .Subject = sSubject
       .TextBody = sBody
       .Send
    End With
End Sub
‘*******************************************************************************
‘* End of Script
‘*******************************************************************************

21 thoughts on “Monitoring Services in Windows Servers via VBScript

  1. Hi Anand,
    Your script is excellent but how to get for multiple servers in a one single mail.Request you to please please say me as i stucked at this point .Please do post the script for mulitple servers or else you can also mail the script to me at bhaskar.v25@gmail.com.I will be waiting for your reply.

    Thanks,
    Bhaskar.

      1. Hi Anand,

        Actually Iam having 80 exchange servers so i want one script to run once for all these 80 servers.Is there a script to run for multiple servers at a time for all these servers.If so i want that script very very urgently because every day i am checking it manually by logging in to the server. And also if that output comes in a single mail it would be very much help full for me.Request you to help me in this issue.

        Thanks,
        Bhaskar.

      2. Hello Anand,

        i tried to exicute the same and getting error in line :38

        Set colItems = objWMIService.ExecQuery(“SELECT * FROM _
        Win32_Service”, “WQL”, _

        Please help me out..innurgen need of it

  2. And main thing i need the script for multiple servers to check the services at a time… Please do help me in this.

    Thanks,
    Bhaskar.

  3. Anand request you to post the script for multiple servers.

    As it is very urgent for me.

    Thanks,
    Bhaskar.

  4. Hi Anand the script is not giving the output,i want a script to run for multiple servers at a time and to check the automatic services are in which state in a single mail.Request your help in this.

    Thanks,
    Bhaskar.

  5. Anand i tried many ways but not getting the output request you to give me the exact script which gives he output..I am waiting its very very urgent..

  6. Hi Anand, i tried many days but not able to get the multiple services script request you to plz plz give me the script

  7. Hi.. I wanted to know if I could use this code in VB6 and if I would have to do anything different to make it work? Thanks…

  8. Is there any script available to monitor auto services and start that service from script if stopped for multiple computers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s