Scripting

用於服務重啟和通過電子郵件發出警報的批處理腳本

  • February 15, 2019

請讓我知道我在程式碼中做錯了什麼。我正在嘗試檢查一組以特定名稱開頭的服務。如果服務處於停止狀態,則觸發電子郵件。我做了一個循環,它將在其中遍歷。但我無法結束那個循環。

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

即使net start命令失敗,您也可能有興趣發送電子郵件。然後,您的腳本可以改進如下(僅給出一個模板): 這種方法應用CALL命令(呼叫子程序)。要退出子常式,請指定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

閱讀重定向文章以了解如何&&||工作。

在所有之前的評論之後編輯2019-02-13 16:14:11Z:以下批處理生成一個沒有任何Microsoft VBScript編譯錯誤.vbs的腳本:

@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

引用自:https://serverfault.com/questions/952606