在发生变化时的批处理文件来监视日志文件目录和发送电子邮件警报? [英] Batch file to monitor log file directory and send email alert when changes occur?
问题描述
我期待创建一个批处理文件,人们可以把一台服务器上,并通过其任务计划程序每天的基础上运行,以监视应用程序的日志文件目录。如果在一个日志文件(错误日志中出现的实例)的名称发生了变化,该批次将发送邮件给管理员或管理员组。
I am looking to create a batch file that one could place on a server and have it run via Task Scheduler on a daily basis to monitor the log file directory of an application. If there is a change in the name of a log file (error log appears for instance), the batch would send out an email to the administrator or administrator group.
什么是一般的脚本这种类型批?
What would be the general script for this type of batch?
感谢您。
推荐答案
我会使用这一个VBScript,因为它有内置的能力。这是一个通用脚本,您可以修改你想要什么。在任务计划程序CSCRIPT调用它。
I'd use a vbscript for this since it has the ability built in. Here is a general purpose script that you can modify to do what you want. Call it with cscript in Task Scheduler.
MonitorFolder()
Function MonitorFolder()
intInterval = "2"
strDrive = "C:"
strFolder = "\\temp\\"
strComputer = "."
Set objWMIService = GetObject( "winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2" )
strQuery = _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'" _
& " And TargetInstance.Path='" & strFolder & "'"
Set colEvents = objWMIService.ExecNotificationQuery (strQuery)
WScript.Echo "Monitoring events...[Ctl-C] to end"
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "Created: " & objTargetInst.Name
SendEmail "FolderMonitor@Domain.com", "youremail@domain.com","Log File Created", "A new error log has appeared"
Case "__InstanceDeletionEvent"
WScript.Echo "Deleted: " & objTargetInst.Name
Case "__InstanceModificationEvent"
WScript.Echo "Modified: " & objTargetInst.Name
End Select
Loop
End Function
Sub SendEmail(sFrom, sTo, sSubject, sMessageBody)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = sSubject
objMessage.From = sFrom
objMessage.To = sTo
objMessage.TextBody = sMessageBody
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
end sub
这篇关于在发生变化时的批处理文件来监视日志文件目录和发送电子邮件警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!