0

I need to create a script which checks if a service is running and starts it if failed. Regardless of the outcome I would like an email which tells me the results. I would like this to be in a .bat file although .vbs is acceptable.

I have found various posts on how to do this. I am currently using:

 set service=###

 for /F "tokens=3 delims=: " %%H in ('sc query %service% ^ | findstr "        STATE"') do      (
if /I "%%H" NEQ "RUNNING" (net start %service%)

This runs successfully and will start the service if stopped however I am getting

%%H was unexpected at this time

This causing it to register as failed on Task scheduler

The second part of the script I am using is:

 for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr "        STATE"') do (
  if /I "%%H" EQ "RUNNING" (
    SendMail /smtpserver localhost /to [email protected] /from [email protected] /subject Service Autostart Notification /body Autostart on service %service% succeded.
  ) else (
    SendMail /smtpserver localhost /to [email protected] /from [email protected] /subject Service Autostart Notification /body Autostart on service %service% failed.
  )
)

)
)

This is not sending the email as "Sendmail is not recognised as an internal or external command, operable command or batch file"

Please could someone give me assistance with this. I have tried to find solutions online but have been unsuccessful thus far.

Kind Regards

8
  • 1
    What is SendMail ? Where is it located ? Commented Jan 29, 2014 at 13:10
  • Did you install Sendmail? It is not a default command on Windows. Commented Jan 29, 2014 at 13:25
  • no I did not install sendmail, please advise. Commented Jan 29, 2014 at 13:30
  • you can download blat -> sourceforge.net/projects/blat/files .Here's an example usage -> blat.net/examples/batch.html Commented Jan 29, 2014 at 13:56
  • I have quite strict security settings on this machine with regards to installing software, I would like to find a way which doesn't require any additional software. I will look into sendmail and blat. Commented Jan 29, 2014 at 14:23

1 Answer 1

0

This batch file allows you to send an email with no third party software, if your IT has not locked down windows scripting host.

Change the port and SSL encryption if your email server needs it - read the first part of the batch file.

You can call the batch file like this:

CALL email.bat "[email protected]" "[email protected]" "Subject line" "Email Body in one line" "smtp.gmail.com"  "[email protected]" "password" "d:\folder\filename to attach.txt"

Email.bat

:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: use these settings to send from a gmail account
:: set port=465 and set SSL=True

:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False

:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables

set Port=25
set SSL=False
set From="[email protected]"
set To="[email protected]"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\file.txt"


:: This section sets the command line arguments
:: use this format:  CALL email.bat "[email protected]" "[email protected]" "Subject line" "Email Body in one line" "smtp.gmail.com"  "[email protected]" "password" "d:\folder\filename to attach.txt"


if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)

set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = %From%
echo >>"%vbsfile%" objEmail.To       = %To%
echo >>"%vbsfile%" objEmail.Subject  = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = %SMTPServer%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = %port%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = %user%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = %pass%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = %SSL%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

cscript.exe /nologo "%vbsfile%"
echo email sent (if variables were correct)
del "%vbsfile%" 2>nul
goto :EOF
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this foxidrive, I will play around with this now. Am I correct in thinking I just put this after the first part of my script? Can this work from the command line with an If command?
Yes, I added the command line above in my answer. It can be added to your script but it would be easier as a standalone batch file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.