1

我有以下代码来监视驱动器。现在我为每个文件创建或删除事件获取 Echo。

有没有办法修改 WScript.Echo 以发送邮件通知?

strDrive = "c"
arrFolders(0) = strDrive & "\\\\"
 strComputer = "." 
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 'Loop throught the array of folders setting up the monitor for Each 
 i = 0 
 For Each strFolder In arrFolders 
     'Create the event sink 
     strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")" 
     ExecuteGlobal strCommand 
     'Setup Notification 
     strQuery = "SELECT * FROM __InstanceOperationEvent WITHIN 1 " & "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" & " and TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
     strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
     ExecuteGlobal strCommand 
     'Create the OnObjectReady Sub 
     strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"
     WScript.Echo strCommand 
     ExecuteGlobal strCommand 
     i = i + 1 
 Next 
 WScript.Echo "Waiting for events..." 

 i = 0 
 While (True) 
     Wscript.Sleep(1000) 
 Wend

而不是像下面的回声:

strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"

我想发送这样的邮件:

strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & VbCrLf & vbTab & "

Set outobj = CreateObject("Outlook.Application")
    Set mailobj = outobj.CreateItem(0)
    With mailobj
        .To = toAddress
        .Subject = Subject
        .HTMLBody = strHTML
        .Send
    End With

" & VbCrLf & "End Sub"

有可能还是有其他方法可以做到这一点..?

4

2 回答 2

4

我不知道您使用什么服务器,但在 Windows 2003 和 2008 上,您可以使用 CDO 对象创建电子邮件。您可以使用智能主机将您的电子邮件发送到。

检查此链接: http: //www.paulsadowski.com/wsh/cdo.htm

您也可以选择任何免费的电子邮件组件来创建电子邮件并使用 smtp 服务器发送您的电子邮件。或者检查这一面,您可以在其中使用包含许多示例的组件:http: //www.chilkatsoft.com/email-activex.asp

** 更新 **

此脚本会根据您的要求检查并发送电子邮件:

strDrive = "d:"
Dim arrFolders(0) : arrFolders(0) = strDrive & "\\\\"
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
'Loop throught the array of folders setting up the monitor for Each 
i = 0 
For Each strFolder In arrFolders 
  'Create the event sink 
  WScript.Echo "setup for folder: " & strFolder & vbLf
  strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")" 
  ExecuteGlobal strCommand
  'Setup Notification 
  strQuery = "SELECT * " _
          & "FROM __InstanceOperationEvent " _
          & "WITHIN 1 " _
          & "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" _
          & "  AND TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
  strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
  ExecuteGlobal strCommand 
  'Create the OnObjectReady Sub 
  strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " &  "objAsyncContext)" & vbLf _
            & "  Wscript.Echo objObject.TargetInstance.PartComponent" & vbLf _
            & "  SendMail(objObject.TargetInstance.PartComponent)" & vbLf _
            & "End Sub"
  'WScript.Echo strCommand 
  ExecuteGlobal strCommand 
  i = i + 1 
Next 

WScript.Echo "Waiting for events..." 
i = 0 
While (True) 
  Wscript.Sleep(1000) 
Wend

Function SendMail(vBody)

  Dim oMail : Set oMail = CreateObject("CDO.Message")

  'Name or IP of Remote SMTP Server
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your.smtp.server"
  oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
  oMail.Configuration.Fields.Update

  oMail.Subject = "Email Watch Info Message"
  oMail.From = "alert@yourdomain.net"
  oMail.To = "target@yourdomain.net"
  oMail.TextBody = vBody
  oMail.Send

End Function

更正发送邮件功能中的设置,您就可以了。

于 2013-03-23T11:34:12.647 回答
1

理论上,VBSendMail DLL 应该可以做你想做的事。

于 2013-03-29T17:50:55.887 回答