现在的位置: 首页 > 综合 > 正文

VB Script判断文件夹下文件的数量并发MAIL预警

2013年10月10日 ⁄ 综合 ⁄ 共 1567字 ⁄ 字号 评论关闭

 

最近用户有个需求,需要监控文件夹下面文件的数量。如果超过一定的阀值需要发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

 

 

抱歉!评论已关闭.