本文共 1452 字,大约阅读时间需要 4 分钟。
最近用户有个需求,需要监控文件夹下面文件的数量。如果超过一定的阀值需要发MAIL预警。从网上找了些VB Script的代码然后自己修改了一下就可以了。
源代码如下:
strMessage = "File number more than10, please check"
strTo= "sample@home.cn"
strFrom="sample@home.cn"
strSubject="File number more than 10,please check"
strSMTPServer="smtp.home.cn"'--smtp地址
strComputer = "."
Set objWMIService =GetObject("winmgmts:\\" & strComputer &"\root\cimv2")
Do While True
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='D:\sample\'} Where " _
& "ResultClass = CIM_DataFile")
If colFileList.Count >= 10 Then
SendMail strFrom,strTo,strSubject,strMessage,strSMTPServer
Exit Do
End If
Wscript.Sleep 600000'--一小时
Loop
' 使用SMTP服务器发送邮件
Function SendMail( strFrom, strSendTo,strSubject, strMessage , strSMTP )
SetoEmail = CreateObject("CDO.Message")
'configuremessage
WithoEmail.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")= 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= strSMTP
.item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")= 0 '不执行验证
.Update
EndWith
'build message
WithoEmail
.From = strFrom
.To = strSendTo
.Subject = strSubject
.TextBody = strMessage
EndWith
'send message
OnError Resume Next
oEmail.Send
IfErr Then
WScript.Echo "SendMail Failed:"& Err.Description
EndIf
End Function
本文转自 lzf328 51CTO博客,原文链接:
http://blog.51cto.com/lzf328/1252922
转载地址:http://xeykm.baihongyu.com/