摘要:近日写一个项目要求计划任务监控另一个一个U盘监控程式是否正常运行,要求使用vbs来写,发现VBS和JS很像,之前写过asp,在此做个备忘记录......
代码: '-------------------------------------------------------------- ' ModifyUser:zxd ' Version: 2021.03.09 - Version 1.0 ' Changelog: create settings file. ’ 参考:https://blog.csdn.net/weixin_34272308/article/details/89796168 '-------------------------------------------------------------- '监控电脑自带[计算器]程序是否正常运行,发送Email通知,在被关闭的情况并重新启动它 Option Explicit '声明所有变量都需要被定义
Dim flag
'计算器是否打开 const testString="calc"
Set shell = CreateObject("wscript.shell") Set tasklist=shell.exec("cmd /c tasklist.exe > C:\Logs\tasklist.txt") '导出进程列表 'shell.run "cmd /c tasklist.exe > C:\USBLoggerSvc\Logs\tasklist.txt",0 '改用run可以隐藏CMD窗口 WScript.Sleep 1000
'读取进程列表每行查找 Set fso = CreateObject("Scripting.FileSystemObject") Set stream = fso.OpenTextFile("C:\Logs\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
'以下这些为Email发送参数设置,可自行查找服务商的SMTP参数修改 Const cdoSendUsingPort=2 Const cdoAnonymous=1 Const serverport=25 Const sendusername="zxd" Const sendpassword="123456"
sender="zxd@126.com" recipient="zxd@gmail.com" 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
End Sub
|
|