質問

I have the following code to monitor a drive. Now I an getting Echo for each file creation or deletion event.

Is there and way to modify the WScript.Echo to send a mail notification?

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

Instead of Echoing like below:

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

I want to send a mail like this:

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"

Is it possible or is there an other way to do this..?

役に立ちましたか?

解決

I don't know what server do you use, but on Windows 2003 and 2008 e.g. you can use CDO object to create a email. You might use a smart host to send your email to.

Check this link: http://www.paulsadowski.com/wsh/cdo.htm

Also you can choose any free email component to create a email and use a smtp server to send your email. Or check this side where you can use a component including many examples how to do it: http://www.chilkatsoft.com/email-activex.asp.

** UPDATED **

This Script checks and send a email as you requestted:

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

Correct the settings in the send mail function and your are fine.

他のヒント

In theory, the VBSendMail DLL should be able to do what you want.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top