Using VBS to realize the code of monitoring process creation and deletion

Time:2019-12-1

Monitors the creation of processes and alerts the temporary event consumer every time a new process is created.

1. Creation of monitoring process

Copy codeThe code is as follows:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colMonitoredProcesses = objWMIService. _
ExecNotificationQuery(“select * from __instancecreationevent ” _
& ” within 1 where TargetInstance isa ‘Win32_Process'”)
i = 0
Do While i = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
Wscript.Echo objLatestProcess.TargetInstance.Name
Loop

2. Monitor the deletion of the process. The temporary event consumer will give an alarm every time the process terminates.

Copy codeThe code is as follows:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colMonitoredProcesses = objWMIService. _
ExecNotificationQuery(“select * from __instancedeletionevent ” _
& “within 1 where TargetInstance isa ‘Win32_Process'”)
i = 0
Do While i = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
Wscript.Echo objLatestProcess.TargetInstance.Name
Loop

3. Monitor the process using the processor

Copy codeThe code is as follows:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colProcesses = objWMIService.ExecQuery _
(“Select * from Win32_process”)
For Each objProcess in colProcesses
sngProcessTime = ( CSng(objProcess.KernelModeTime) + _
CSng(objProcess.UserModeTime)) / 10000000
Wscript

Reference link:

  1. Monitoring process creation
  2. Monitoring process deletion

Reference: http://hi.baidu.com/% B1% F9% d0% C4% B3% D5 / blog / item / 5691a1c3146ded55b219a861.html http://demon.tw/programming/vbs-monitoring-process-creation-and-deletion.html