最近用户有个需求,需要监控文件夹下面文件的数量。如果超过一定的阀值需要发MAIL预警。从网上找了些VBScript的代码然后自己修改了一下就可以了。
源代码如下:
strMessage="Filenumbermorethan10,pleasecheck"
strTo="sample@home.cn"
strFrom="sample@home.cn"
strSubject="Filenumbermorethan10,pleasecheck"
strSMTPServer="smtp.home.cn"'--smtp地址
strComputer="."
SetobjWMIService=GetObject("winmgmts:\\"&strComputer&"\root\cimv2")
DoWhileTrue
SetcolFileList=objWMIService.ExecQuery_
("ASSOCIATORSOF{Win32_Directory.Name='D:\sample\'}Where"_
&"ResultClass=CIM_DataFile")
IfcolFileList.Count>=10Then
SendMailstrFrom,strTo,strSubject,strMessage,strSMTPServer
ExitDo
EndIf
Wscript.Sleep600000'--一小时
Loop
'使用SMTP服务器发送邮件
FunctionSendMail(strFrom,strSendTo,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
'buildmessage
WithoEmail
.From=strFrom
.To=strSendTo
.Subject=strSubject
.TextBody=strMessage
EndWith
'sendmessage
OnErrorResumeNext
oEmail.Send
IfErrThen
WScript.Echo"SendMailFailed:"&Err.Description
EndIf
EndFunction