用于服务重启和通过电子邮件发出警报的批处理脚本

用于服务重启和通过电子邮件发出警报的批处理脚本

请告诉我我的代码哪里做错了。我正在尝试检查一组以特定名称开头的服务。如果服务处于停止状态,则触发电子邮件。我创建了一个循环,它将在其中遍历。但我无法结束该循环。

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

即使命令失败,您可能也想发送电子邮件net start。那么,您的脚本可以改进如下(仅给出一个模板):此方法适用于CALL命令(调用子程序). 要退出子程序请指定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

阅读重定向文章解释如何&&以及||工作原理。

编辑注释所有之前之后2019-02-13 16:14:11Z:以下批处理生成一个.vbs脚本,该脚本免于任何微软 VBScript汇编错误

@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

相关内容