0

Please let me know what i am doing wrong in my code. I am trying to check a set of Services starting with specific set of name. And triggering an email if service is in stop state. I have made a loop where in it will traverse. But i am not able to end that loop.

setlocal :loop @for /f "tokens=1*" %%a in ('sc queryex state^=inactive ^| findstr /r /c:"DISPLAY_NAME:.*ASP"') do net start "%%b" && goto :mail :mail 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="smtp.gmail.com" set User="[email protected]" set Pass="1234567" 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 :loop 
1
  • If my answer was helpful, please consider marking it as accepted. See this page for an explanation of why this is important. Commented Apr 19, 2019 at 12:14

1 Answer 1

0

You may be interested in sending an email even if the net start command fails. Then, your script could be improved as follows (given merely a template): This approach applies CALL command (call a subroutine). To exit the subroutine specify GOTO:eof:

@ECHO OFF SETLOCAL EnableExtensions 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="smtp.gmail.com" set User="[email protected]" set Pass="1234567" 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" ) @for /f "tokens=1*" %%a in (' sc queryex state^=inactive ^| findstr /r /c:"DISPLAY_NAME:.*ASP" ') do ( set "aServiceFound=%%b" REM Success or failure are based on the Exit Code of the `net start` command net start "%%b" && set "aServiceLuck=started" || set "aServiceLuck=failed" CALL :mail ) goto :endloop :mail REM Handle `net start` success/failure here REM add %aServiceFound% and %aServiceLuck% to the email subject and/or body set Subject="%aServiceLuck%: %aServiceFound% %Subject%" set Body="%aServiceLuck%: %aServiceFound% %Body%" set "vbsfile=%temp%\email-bat.vbs" del "%vbsfile%" 2>nul REM script continues here (creating and running the %vbsfile% script) … echo email sent (if variables were correct) del "%vbsfile%" 2>nul REM goto :loop goto :eof :endloop 

Read the Redirection article for explanation how && and || work.

Edit after comments all before 2019-02-13 16:14:11Z: The following batch generates a .vbs script which is free of any Microsoft VBScript compilation error:

@ECHO OFF SETLOCAL EnableExtensions REM set "Port=25" => error "530 5.7.0 Must issue a STARTTLS command first" set "Port=465" set "SSL=False" set "[email protected]" set "[email protected]" set "Subject=Subject line" set "Body=Email Body in one line" set "SMTPServer=smtp.gmail.com" set "[email protected]" set "Pass=1234567" @for /f "tokens=1*" %%a in (' sc queryex state^=inactive ^| findstr /r /c:"DISPLAY_NAME:.*ASP" ') do ( set "aServiceFound=%%b" REM Success or failure are based on the Exit Code of the `net start` command 2>NUL net start "%%b" && set "aServiceLuck=started" || set "aServiceLuck=failed" CALL :mail ) goto :endloop :mail REM Handle `net start` success/failure here REM add %aServiceFound% and %aServiceLuck% to the email subject and/or body set "Subject=%aServiceLuck%: %aServiceFound% %Subject%" set "Body=%aServiceLuck%: %aServiceFound% %Body%" 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 rem `Wscript.Echo` merely for debugging purposes echo >>"%vbsfile%" Wscript.Echo "Email Sent" cscript.exe //nologo "%vbsfile%" goto :eof :endloop 
6
  • Hi JosefZ, Thanks for the revert but i am getting the below error. Commented Feb 13, 2019 at 8:24
  • C:\Users\genesys\AppData\Local\Temp\email-bat.vbs(5, 56) Microsoft VBScript compilation error: Expected end of statement Commented Feb 13, 2019 at 8:25
  • @Anonymous take a look into the %temp%\email-bat.vbs. Note that it's a better practice to define a variable using double quotes as follow: set "Subject=Subject line" and then use it's value (which isn't double-quoted in that case) double-quoted if necessary e.g. as follows: >>"%vbsfile%" echo objEmail.Subject = "%Subject%". Commented Feb 13, 2019 at 15:13
  • i didnt get you...@Josefz.. Commented Feb 13, 2019 at 16:14
  • @Anonymous Simply check the 5th line in the .vbs script for unbalanced double quotes using e.g. notepad "%temp%\email-bat.vbs" (use your preferred plain text / code editor instead notepad.exe). Answer updated. Commented Feb 15, 2019 at 20:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.