Home / Microsoft / Microsoft DOS / help - batch file to send automated email when windows service stops
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: help - batch file to send automated email when windows service stops  (Read 6763 times)
29april
Topic Starter
Greenhorn



Posts: 6


« on: December 04, 2009, 02:52:03 PM »

hi, I need to create a batch file that sends an email automatically when a windows service stops.

I could have the batch file run every 10 minutes to see if the service is active, but I have no idea how to give an if condition and then send an email. First of all is this possible? any suggestions would be great to know. Thanks.
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #1 on: December 04, 2009, 03:15:01 PM »

Well you can't send emails with batch, and I don't have much experience with windows services, so I suggest using another language maybe.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
gh0std0g74
Apprentice



Thanked: 37
Posts: 590


« Reply #2 on: December 04, 2009, 04:37:35 PM »

hi, I need to create a batch file that sends an email automatically when a windows service stops.

I could have the batch file run every 10 minutes to see if the service is active, but I have no idea how to give an if condition and then send an email. First of all is this possible? any suggestions would be great to know. Thanks.

here's an example vbscript to check service is up
Code: [Select]
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objService in colRunningServices
    If objService.DisplayName = "Print Spooler" And objService.State = "Running" Then
    WScript.Echo "Print Spooler is Running"
        'SEnd email here.
    End If
Next
for sending email, you can either do it with vbscript (search google for "vbscript email") and there are code examples to do that. OR you can use blat.exe (search google for download). For batch method, you can use sc. check sc /?  for more.
IP logged

gh0std0g74
Apprentice



Thanked: 37
Posts: 590


« Reply #3 on: December 04, 2009, 04:38:55 PM »

Well you can't send emails with batch,
yes you can.
IP logged

Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #4 on: December 04, 2009, 04:53:55 PM »

yes you can.
I mean without downloading 3rd party programs. Anything that has a command-line opyion can be run with batch.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
29april
Topic Starter
Greenhorn



Posts: 6


« Reply #5 on: December 04, 2009, 04:54:42 PM »

gh0std0g74,

Thanks I'll search for sample VB scripts to send email as well. but how do I call this VB script from a batch file? Please let me know, I think that would solve my problem :)
IP logged
29april
Topic Starter
Greenhorn



Posts: 6


« Reply #6 on: December 04, 2009, 05:03:11 PM »

Also, is there a way for batch file to do it independently (without use of VB scripts)? Installing VB might be a long process
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #7 on: December 04, 2009, 05:31:37 PM »

You already have it installed. There is a cscript program that lets you run vbs files.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
29april
Topic Starter
Greenhorn



Posts: 6


« Reply #8 on: December 04, 2009, 06:00:47 PM »

Thanks Helpmeh, can u let me know the code u use in batch file to call the vb script?
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,930

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #9 on: December 04, 2009, 06:17:22 PM »

Some reading to do.  8)
Quote
Visual Basic Scripting Edition
VBScript User's Guide
The VBScript User's Guide includes the following sections which explain the concept of Visual Basic Scripting Edition (VBScript) and how to use it.
http://msdn.microsoft.com/en-us/library/sx7b3k7y(VS.85).aspx
IP logged

gh0std0g74
Apprentice



Thanked: 37
Posts: 590


« Reply #10 on: December 04, 2009, 07:38:31 PM »

gh0std0g74,

Thanks I'll search for sample VB scripts to send email as well. but how do I call this VB script from a batch file? Please let me know, I think that would solve my problem :)

Code: [Select]
c:\test > cscript /nologo myscript.vbs
IP logged

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #11 on: December 05, 2009, 01:46:28 AM »

Quote
c:\test > cscript /nologo myscript.vbs

//nologo with 2 slashes is preferred

IP logged


Proud to be European
29april
Topic Starter
Greenhorn



Posts: 6


« Reply #12 on: December 05, 2009, 02:16:00 AM »

thanks for your superb support, people.. Im not done yet but I guess I got all what I wanted to know :-)
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #13 on: December 05, 2009, 02:44:51 AM »

Used without parameters, NET START returns a list of services.

Code: [Select]
C:\>net start
These Windows services are started:

   Application Layer Gateway Service
   Avira AntiVir Scheduler
   COM+ Event System
   COM+ System Application
   Computer Browser
   Cryptographic Services
   DCOM Server Process Launcher
   DHCP Client
   Distributed Link Tracking Client
   DNS Client
   Event Log
   Fast User Switching Compatibility
   Folder Size
   Help and Support
   IPSEC Services
   Java Quick Starter
   Logical Disk Manager
   MS Software Shadow Copy Provider
   Network Connections
   Network Location Awareness (NLA)
   Plug and Play
   Print Spooler
   Protected Storage
   Rapport Management Service
   Remote Procedure Call (RPC)
   Remote Procedure Call (RPC) Locator
   Remote Registry
   Secondary Logon
   Security Accounts Manager
   Security Center
   Server
   Shell Hardware Detection
   SSDP Discovery Service
   System Event Notification
   Task Scheduler
   TCP/IP NetBIOS Helper
   Terminal Services
   Themes
   VMware vCenter Converter Server
   Volume Shadow Copy
   WebClient
   Windows Audio
   Windows Firewall/Internet Connection Sharing (ICS)
   Windows Image Acquisition (WIA)
   Windows Management Instrumentation
   Windows Time
   Windows User Mode Driver Framework
   Workstation

The command completed successfully.

In a batch you can use this with a pipe to FIND and the && (errorlevel=0) and  || (errorlevel > 0) operators to see if a service is running or is not running and do something in those situations
Code: [Select]
set ServiceName=Java Quick Starter

net start | FIND "%ServiceName%" && (
    echo The %ServiceName% service is running"
    REM other commands
    REM other commands
    REM etc
    )

net start | FIND "%ServiceName%" || (
    echo The %ServiceName% service is NOT running
    REM other commands
    REM other commands
    REM etc
    )
IP logged


Proud to be European
gh0std0g74
Apprentice



Thanked: 37
Posts: 590


« Reply #14 on: December 05, 2009, 05:20:14 AM »

//nologo with 2 slashes is preferred


yes. I always put /nologo after cscript.exe and its ok that way.  Up to the individual until M$hit makes it mandatory for  double slash syntax once and for all.
IP logged

Pages: [1] 2  All - (Top) Print 
Home / Microsoft / Microsoft DOS / help - batch file to send automated email when windows service stops « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.103 seconds with 20 queries.