脚本如果没有则运行电子邮件结果启动服务 [英] Script to start service if not running then email results

查看:375
本文介绍了脚本如果没有则运行电子邮件结果启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个脚本服务是否运行的检查,如果没有启动它。无论结果如何,我想告诉我结果的电子邮件。我想这是一个.bat文件,虽然.VBS是可以接受的。

I need to create a script which checks if a service is running and starts it if failed. Regardless of the outcome I would like an email which tells me the results. I would like this to be in a .bat file although .vbs is acceptable.

我已经找到了如何做到这一点各个岗位。我目前使用的:

I have found various posts on how to do this. I am currently using:

 set service=###

 for /F "tokens=3 delims=: " %%H in ('sc query %service% ^ | findstr "        STATE"') do      (
if /I "%%H" NEQ "RUNNING" (net start %service%)

这成功运行,如果停止了但我越来越会启动该服务

This runs successfully and will start the service if stopped however I am getting

%%H was unexpected at this time

这是造成失败的任务调度它来注册

This causing it to register as failed on Task scheduler

我使用的脚本的第二部分是:

The second part of the script I am using is:

 for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr "        STATE"') do (
  if /I "%%H" EQ "RUNNING" (
    SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% succeded.
  ) else (
    SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% failed.
  )
)

)
)

这是不是发送电子邮件作为Sendmail的不被识别为一个内部或外部命令,可操作的命令或批处理文件

This is not sending the email as "Sendmail is not recognised as an internal or external command, operable command or batch file"

请能有人给我这个援助。我试图在网上找解,但没有成功迄今。

Please could someone give me assistance with this. I have tried to find solutions online but have been unsuccessful thus far.

亲切的问候

推荐答案

这个批处理文件允许您没有第三方软件发送电子邮件,如果您还没有锁定了Windows脚本宿主。

This batch file allows you to send an email with no third party software, if your IT has not locked down windows scripting host.

更改端口和SSL加密,如果您的电子邮件服务器需要它 - 浏览批处理文件的第一部分。

Change the port and SSL encryption if your email server needs it - read the first part of the batch file.

您可以这样调用批处理文件:

You can call the batch file like this:

CALL email.bat "myname@gmail.com" "RecipientEmailAddress@server.com" "Subject line" "Email Body in one line" "smtp.gmail.com"  "myname@gmail.com" "password" "d:\folder\filename to attach.txt"

Email.bat

Email.bat

:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: use these settings to send from a gmail account
:: set port=465 and set SSL=True

:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False

:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables

set Port=25
set SSL=False
set From="myemail@myemailserver.com"
set To="recipient@server.com"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\file.txt"


:: This section sets the command line arguments
:: use this format:  CALL email.bat "myname@gmail.com" "RecipientEmailAddress@server.com" "Subject line" "Email Body in one line" "smtp.gmail.com"  "myname@gmail.com" "password" "d:\folder\filename to attach.txt"


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 :EOF

这篇关于脚本如果没有则运行电子邮件结果启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆