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="abc@gmail.com"
set To="abc@gmail.com"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="smtp.gmail.com"
set User="abc@gmail.com"
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
Anonymous
  • 5
  • 2
  • If my answer was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. – JosefZ Apr 19 '19 at 12:14

1 Answers1

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="abc@gmail.com"
set To="abc@gmail.com"
set "Subject=Subject line"
set "Body=Email Body in one line"
set SMTPServer="smtp.gmail.com"
set User="abc@gmail.com"
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 "From=abc@gmail.com"
set "To=abc@gmail.com"
set "Subject=Subject line"
set "Body=Email Body in one line"
set "SMTPServer=smtp.gmail.com"
set "User=abc@gmail.com"
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
JosefZ
  • 1,564
  • 1
  • 10
  • 18
  • Hi JosefZ, Thanks for the revert but i am getting the below error. – Anonymous Feb 13 '19 at 08:24
  • C:\Users\genesys\AppData\Local\Temp\email-bat.vbs(5, 56) Microsoft VBScript compilation error: Expected end of statement – Anonymous Feb 13 '19 at 08: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%"`. – JosefZ Feb 13 '19 at 15:13
  • i didnt get you...@Josefz.. – Anonymous Feb 13 '19 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. – JosefZ Feb 15 '19 at 20:45
  • Thanks All...I have utilized the answer,there is an upgrade in above question. For now what if there are multiple services in inactive state, but usually mail is triggered only for the service. – Anonymous Apr 19 '19 at 11:48