有C:/Document1和C:/Document2这样两个文件夹,里面分别有一个相同的Exe文件E1和E2,它的作用是一定被执行就处理该文件夹下的文件。
需求:不可以修改Exe文件,通过一个程序P1来完成:
- 定时运行(Exe文件中没有定时功能)。
- P1运行,保证E1和E2都要运行,即如果运行了就要保持,如果没有运行的话就要启动Exe。
- 写入日志。需要写入如下内容:
------------------------------------------------
Process ID: 3036
Process Path:
Time Created: 2/14/2007 11:04:53 AM
Time Deleted: 2/14/2007 11:05:23 AM
Duration: 30 seconds
------------------------------------------------
启动了几个就要写入几个。
Note: 进程名称相同,路径不同,不能修改Exe文件。
方案:通过一个将一个VBS程序添加到Task Schedule中来定时地执行。通过VBS可以实现对Process的查询,以及对Process的create和delete的情况进行记录。
实现代码如下:
On
error
resume
next
' The path of the executable.
' The first path is as usual.
' The second path is on the C disk.(You can copy the notepad.exe from system32 and run it.)
Dim strPath1
Dim strPath2
' The name of the process.
Dim strNameOfProcess
' The interval string between different log data.
Dim strInterval
' An Integer
' 0 The process don't exists in the Windows Task Manager
' 1 Exist one of two.
' 2 Both exist.
Dim intWhetherExist
' The path of the process need to be fun.
Dim strExePath
' The ID of the process need to be fun.
Dim intExeID
' Save the match relationship between ProcessID and ExecutablePath in this array.
Dim arrayProcessIDandExecutablePath( 1 , 1 )
' The path of the log file.
Dim strLogPath
' --------------------------------------------------------------------------------
' Take notepad.exe for example. Open just one notepad.
strPath1 = " C:WINNTSystem32 otepad.exe "
strPath2 = " C: otepad.exe "
strNameOfProcess = " notepad.exe "
strInterval = " ------------------------------------------------ "
strLogPath = " c: estfile.txt "
' --------------------------------------------------------------------------------
' Default is zero.
intWhetherExist = 0
' Create an array contains the name of all processes need to be found.
strComputer = " . "
arrTargetProcs = Array (strNameOfProcess)
' ---------------------------------------------------------------
' 1. Find the process we want.
' ---------------------------------------------------------------
' Find the process in the name list.
Set objWMIService = GetObject ( " winmgmts: "
' The path of the executable.
' The first path is as usual.
' The second path is on the C disk.(You can copy the notepad.exe from system32 and run it.)
Dim strPath1
Dim strPath2
' The name of the process.
Dim strNameOfProcess
' The interval string between different log data.
Dim strInterval
' An Integer
' 0 The process don't exists in the Windows Task Manager
' 1 Exist one of two.
' 2 Both exist.
Dim intWhetherExist
' The path of the process need to be fun.
Dim strExePath
' The ID of the process need to be fun.
Dim intExeID
' Save the match relationship between ProcessID and ExecutablePath in this array.
Dim arrayProcessIDandExecutablePath( 1 , 1 )
' The path of the log file.
Dim strLogPath
' --------------------------------------------------------------------------------
' Take notepad.exe for example. Open just one notepad.
strPath1 = " C:WINNTSystem32 otepad.exe "
strPath2 = " C: otepad.exe "
strNameOfProcess = " notepad.exe "
strInterval = " ------------------------------------------------ "
strLogPath = " c: estfile.txt "
' --------------------------------------------------------------------------------
' Default is zero.
intWhetherExist = 0
' Create an array contains the name of all processes need to be found.
strComputer = " . "
arrTargetProcs = Array (strNameOfProcess)
' ---------------------------------------------------------------
' 1. Find the process we want.
' ---------------------------------------------------------------
' Find the process in the name list.
Set objWMIService = GetObject ( " winmgmts: "