1

ドライブを監視するための次のコードがあります。これで、ファイルの作成または削除イベントごとにエコーを取得します。

メール通知を送信するように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

理論的には、VBSendMailDLLは必要な処理を実行できるはずです。

于 2013-03-29T17:50:55.887 に答える