Announcement

Collapse
No announcement yet.

Script to check any service status and send an email with status

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to check any service status and send an email with status

    Hi guys !

    I want to create a script to check any service status and send an email with status

    For example:
    Id like to add this script on script logon to check if Antivirus service is running... if not running it must send an email to Support Team

    anybody help me ?

    Thanks !!!

  • #2
    Re: Script to check any service status and send an email with status

    Hi,

    My scripting is not something I am proud of but I'll try to do my bit to get you started:

    One way of doing it is to use the SC.EXE and pipe the output as a text file on a network drive (Someone could extend on this one to do the e-mail notification)

    Code:
    sc query <Servicename> > \\server\share\%computername%.txt
    and the second option is to use SC again with the failure parameter to configure the relevant service to start the following VBS script upon failure:

    Code:
    Set objEmail = CreateObject("CDO.Message")
    
    objEmail.From = "[email protected]"
    objEmail.To = "[email protected]"
    objEmail.Subject = "Server down" 
    objEmail.Textbody = "Server1 is no longer accessible over the network."
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
            "smarthost" 
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
    http://www.microsoft.com/technet/scr....mspx?mfr=true

    Ta
    Caesar's cipher - 3

    ZKHQ BRX HYHQWXDOOB GHFLSKHU WKLV BRX ZLOO UHDOLVH LW ZDV D ZDVWH RI WLPH!

    SFX JNRS FC U6 MNGR

    Comment


    • #3
      Re: Script to check any service status and send an email with status

      Use the Win32_Service class from msdn: http://msdn.microsoft.com/en-us/library/aa394418.aspx

      An example is at the bottom of that link. Is this just for one service? Or are you going to get the status from multiple services on the same machine?

      Comment


      • #4
        Re: Script to check any service status and send an email with status

        EDIT** I made a change to only send the email if the service is not running...I didn't read your whole post. Edit is in green.

        I haven't tested this, but something like this should work.

        Code:
        strComputer = "."
        strService = "Whatever the service name is"
        
        Set objEmail = CreateObject("CDO.Message")
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
                For Each objItem in colItems
                     If objItem.status <>"OK" or objItem.state <>"Running" then
                          objEmail.From = "[email protected]"
                          objEmail.To = "[email protected]"
                          objEmail.Subject = "Antivirus State" 
                          objEmail.Textbody = objItem.DisplayName & " on: "  & strComputer _
                               " State: "  & objItem.State & VbCrLf
                          objEmail.Configuration.Fields.Item _
                             ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                         objEmail.Configuration.Fields.Item _
                              ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                                "smarthost" 
                         objEmail.Configuration.Fields.Item _
                                 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                         objEmail.Configuration.Fields.Update
                         objEmail.Send
                   End If
                Next
        Last edited by ekrengel; 21st March 2009, 13:46. Reason: Only email if service is not running

        Comment


        • #5
          Re: Script to check any service status and send an email with status

          Hey man,

          I have tested this script, but it does not work when Symantec service is stopped... any idea ?


          strComputer = "."
          strService = "Symantec Antivirus"

          Set objEmail = CreateObject("CDO.Message")
          Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
          Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
          For Each objItem in colItems
          If objItem.status <>"OK" or objItem.state <>"Running" then
          objEmail.From = "[email protected]work.com"
          objEmail.To = "[email protected]"
          objEmail.Subject = "Antivirus State"
          objEmail.Textbody = objItem.DisplayName & " on: " & strComputer _
          '" State: " & objItem.State & VbCrLf
          objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
          objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
          "smtp.work.com"
          objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
          objEmail.Configuration.Fields.Update
          objEmail.Send
          End If
          Next

          Comment


          • #6
            Re: Script to check any service status and send an email with status

            Is that the actual name of the service in "services"?

            Comment


            • #7
              Re: Script to check any service status and send an email with status

              Code:
              If objItem.status <>"OK" or objItem.state <>"Running" then
              Just a thought, shouldn't that be "Stopped or s'thing"?

              Ta
              Caesar's cipher - 3

              ZKHQ BRX HYHQWXDOOB GHFLSKHU WKLV BRX ZLOO UHDOLVH LW ZDV D ZDVWH RI WLPH!

              SFX JNRS FC U6 MNGR

              Comment


              • #8
                Re: Script to check any service status and send an email with status

                Originally posted by ekrengel View Post
                Is that the actual name of the service in "services"?
                yes !!

                it show in Exibithion name: Symantec AntiVirus

                Comment


                • #9
                  Re: Script to check any service status and send an email with status

                  Originally posted by L4ndy View Post
                  Code:
                  If objItem.status <>"OK" or objItem.state <>"Running" then
                  Just a thought, shouldn't that be "Stopped or s'thing"?

                  Ta

                  I have tried this way, but does not work too...

                  Comment


                  • #10
                    Re: Script to check any service status and send an email with status

                    Originally posted by gorlandini View Post
                    yes !!

                    it show in Exibithion name: Symantec AntiVirus

                    if you dont have symantec and you like to test this script you can try the print spooler service... I believe if it work for one it will work for all

                    Comment


                    • #11
                      Re: Script to check any service status and send an email with status

                      Originally posted by L4ndy View Post
                      Code:
                      If objItem.status <>"OK" or objItem.state <>"Running" then
                      Just a thought, shouldn't that be "Stopped or s'thing"?

                      Ta
                      The vbscript operator "<>" means does not equal...so that should be the correct statement.

                      http://old.ournet.md/~vladuxx/vbs13.htm

                      gorlandini - I had it backwards. Replace this:
                      Code:
                      If objItem.status <>"OK" or objItem.state <>"Running" then
                      With this:

                      Code:
                      If (objItem.state <> "Running" or objItem.status <> "OK") Then

                      Comment


                      • #12
                        Re: Script to check any service status and send an email with status

                        Oops, that's not the problem

                        That is still the same statement haha. "strService" wasn't in the select statement. Sorry!

                        Code:
                        strComputer = "."
                        strService = "Whatever the service name is"
                        
                        Set objEmail = CreateObject("CDO.Message")
                        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                        Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & strService & "'")
                                For Each objItem in colItems
                                     If (objItem.state <> "Running" or objItem.status <> "OK") Then
                                          objEmail.From = "[email protected]"
                                          objEmail.To = "[email protected]"
                                          objEmail.Subject = "Antivirus State" 
                                          objEmail.Textbody = objItem.DisplayName & " on: "  & strComputer _
                                               " State: "  & objItem.State & VbCrLf
                                          objEmail.Configuration.Fields.Item _
                                             ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                                         objEmail.Configuration.Fields.Item _
                                              ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                                                "smarthost" 
                                         objEmail.Configuration.Fields.Item _
                                                 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                                         objEmail.Configuration.Fields.Update
                                         objEmail.Send
                                   End If
                                Next

                        Comment


                        • #13
                          Re: Script to check any service status and send an email with status

                          Yeah, that works for me now.
                          Cheers Ekrengel, got there eventually
                          I'll put this one on my toolbelt.
                          Caesar's cipher - 3

                          ZKHQ BRX HYHQWXDOOB GHFLSKHU WKLV BRX ZLOO UHDOLVH LW ZDV D ZDVWH RI WLPH!

                          SFX JNRS FC U6 MNGR

                          Comment


                          • #14
                            Re: Script to check any service status and send an email with status

                            Hey Friends,

                            Its working now !!! many thanks....

                            but the msg body do not show the computer name... show me only "." as follow:

                            "Symantec AntiVirus on: ."

                            do you know how can we fix it ?

                            Comment


                            • #15
                              Re: Script to check any service status and send an email with status

                              Yes, put this in the front of the script so it looks like this:

                              Code:
                              Set objNetwork = CreateObject("WScript.Network")
                              strComputer = objNetwork.ComputerName
                              strService = "Whatever the service name is"

                              Comment

                              Working...
                              X