批处理脚本发送电子邮件

时间:2019-02-06 13:49:09

标签: batch-file scripting

请让我知道我在代码中做错了什么。我正在尝试检查一组以特定名称开头的服务。并在服务处于停止状态时触发电子邮件。我做了一个循环,它将在其中遍历。但是我无法结束该循环。

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

1 个答案:

答案 0 :(得分:0)

SELECT
   COUNT(pc.post_id) AS  num_comments 
   ,p.* FROM posts p
   LEFT OUTER JOIN posts_comments pc ON p.id = pc.post_id
GROUP BY p.id
ORDER BY 1 DESC
LIMIT $1
OFFSET $2

变量@echo off setlocal rem Count variable for inactive services. set "ServicesInactive=0" rem Increment count variable to number of start services. for /f "tokens=1*" %%a in ('sc queryex state^=inactive ^| findstr /r /c:"DISPLAY_NAME:.*ASP"') do net start "%%b" && set /a "ServicesInactive+=1" rem Send email if count is greater than zero. if %ServicesInactive% gtr 0 goto :mail goto :endmail :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" set cdoSchema=http://schemas.microsoft.com/cdo/configuration ( echo Set objArgs = WScript.Arguments echo Set objEmail = CreateObject("CDO.Message") echo objEmail.From = %From% echo objEmail.To = %To% echo objEmail.Subject = %Subject% echo objEmail.Textbody = %body% if exist %fileattach% echo objEmail.AddAttachment %fileattach% echo with objEmail.Configuration.Fields echo .Item ("%cdoSchema%/sendusing") = 2 ' not local,smtp echo .Item ("%cdoSchema%/smtpserver") = %SMTPServer% echo .Item ("%cdoSchema%/smtpserverport") = %port% echo .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic echo .Item ("%cdoSchema%/sendusername") = %user% echo .Item ("%cdoSchema%/sendpassword") = %pass% echo .Item ("%cdoSchema%/smtpusessl") = %SSL% echo .Item ("%cdoSchema%/smtpconnectiontimeout") = 30 echo .Update echo end with echo objEmail.Send ) > "%vbsfile%" cscript.exe /nologo "%vbsfile%" echo email sent (if variables were correct) del "%vbsfile%" 2>nul :endmail ServicesInactiveset,每个服务 在0循环中启动将使for增加ServicesInactive

完成1循环后,如果for大于 ServicesInactive,然后将通过命令0发送一封电子邮件,否则 将goto :email

移动了括号之间的回声,从而使写入 临时VBS文件更加整洁高效。