如何编写批处理文件错误/返回的免费通知脚本?

时间:2020-03-06 14:44:22  来源:igfitidea点击:

我在停止服务并重新启动它时遇到问题,希望在进程运行时收到通知,让我知道结果是什么。

这是场景
我有一个" sc"命令的文本文件输出。我想发送该文件,但不作为附件发送。另外,我想快速查看电子邮件主题中的初始状态。

这是" servstop.txt"文件的内容:

[SC] StartService FAILED 1058:
  
  The service cannot be started, either
  because it is disabled or because it
  has no enabled devices associated with
  it.

我希望电子邮件的主题为" Alert Service Start:[SC] StartService FAILED 1058"
以及包含上述整个错误消息的正文。

我将使用称为blat的程序向我发送当前结果,并在下面的答案中向我发送结果。

解决方案

这就是我的做法。
首先,我收到blat邮件(公共域smtp邮件程序)并将其放入d:\ blat目录。我的Exchange服务器允许我发送不带ID /密码的电子邮件,只是假定我是blat命令的"发件人"字段中的那个人。

@echo off
sc start Alerter >servstop.txt
SetLocal EnableDelayedExpansion
set content=
set subj=
for /F "delims=" %%i in (servstop.txt) do set content=!content! %%i  
for /f "tokens=1 delims=:" %%s in ("%content%") do set subj=%%s 

d:\blat\blat.exe -body "%content%" -to [email protected] -f [email protected] -server smtp.foo.bar -s "Alert Service Start:%subj% " -log d:\blat\blat.log
EndLocal

Splunk看起来很有前途。还没有尝试过。来自网站的两个关于引用和警告的块引用如下。

INDEX: With a variety of flexible input methods you can index logs, configurations, traps and alerts, messages, scripts, and code and performance data from all your applications, servers and network devices. Monitor file systems for scripts and configuration changes, capture archive files, find and tail live application logs, connect to network ports to receive syslog, SNMP and other network-based instrumentation. And this is just where it starts.
  
  ALERT: Any search can be run on a schedule and trigger notifications or actions based on the search results. And because it works across different components and technologies, Splunk is the most flexible monitoring tool in your arsenal. Notifications can be sent via email, RSS or SNMP to other management consoles. Actions trigger scripts performing user described activities like restarting an application, server or network device.