监控程序进程的VBS脚本

监控计算器程序是否处于运行状态,只需更改testString常量的值,就可以通过自己的邮箱知道该程序目前的运行状况。
Option Explicit

Dim flag

const testString="calc"

Set shell = CreateObject("wscript.shell")
Set tasklist=shell.exec("cmd /c tasklist.exe > c:\vbs\tasklist.txt")

WScript.Sleep 1000

Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("c:\vbs\tasklist.txt")
Do While Not stream.AtEndOfStream
   str=stream.ReadLine
   If Len(Trim(str))>0 Then process str
Loop
stream.Close

testmail flag


Sub process(str)
     If InStr(1,str,testString,1)<>0 Then
        flag=1
     End If
     
End Sub



Sub testmail(c)

Dim sender,recipient,relayserver,msg,nfiles,conf,arg,prefix,send_errno,plural,connectiontimeout

'If WScript.Arguments.count<=0 Then
    'MsgBox"Usage: mailfiles filename..., or drag files onto shortcut"
    'WScript.Quit 0
'End If

Const cdoSendUsingPort=2
Const cdoAnonymous=1
Const serverport=25
Const sendusername="abc"
Const sendpassword="123456"

recipient="[email protected]"
relayserver="smtp.126.com"

Set msg=CreateObject("cdo.message")
Set conf=CreateObject("cdo.configuration")
Set msg.Configuration=conf

If c=1 then
With msg 
     .To=recipient
     .From=sender
     .Subject="Program is runing"
     .TextBody=testString & " is running"
     
     nfiles=0
     For Each arg In WScript.Arguments
         .AddAttachment arg
         nfiles=nfiles+1
     Next
End With
Else
With msg 
     .To=recipient
     .From=sender
     .Subject="Program is closed"
     .TextBody=testString & " is closed"
     
     nfiles=0
     For Each arg In WScript.Arguments
         .AddAttachment arg
         nfiles=nfiles+1
     Next
End With
shell.exec("c:\windows\system32\calc.exe")
End If
 

prefix="http://schemas.microsoft.com/cdo/configuration/"
With conf.Fields
     .Item(prefix&"sendusing")=cdosendusingport
     .Item(prefix&"smtpserver")=relayserver
     .Item(prefix&"smtpserverport") = serverport
     .Item(prefix&"smtpauthenticate")=cdoAnonymous
     .item(prefix&"smtpconnectiontimeout") = connectiontimeout
     .Item(prefix&"sendusername")=sendusername
     .Item(prefix&"sendpassword")=sendpassword
     .Update
End With

On Error Resume Next
msg.Send
'send_errno=""
'send_errno=Err.Number
'On Error Goto 0
'If send_errno<>0 Then
   'MsgBox "error sending message" & "  " & Err.Number & "  " & Err.Description
'Else
   'If nfiles=1 Then plural="" Else plural="s"
   'MsgBox "Sent" & nfiles & " file" & plural & " to " & recipient
'End If

End Sub

你可能感兴趣的:(邮件,程序,监控,vbs)