How VBScript monitors and terminates a specified process
This article mainly shows you "how VBScript monitors and ends the specified process". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how VBScript monitors and ends the specified process".
Run the effect diagram:
Code (monprocess.vbs):
The copy code is as follows:
On Error Resume Next
StrComputer = "."
ArrTargetProcs = Array ("calc.exe", "notepad.exe", "other.exe")
'The name of the process to be monitored in the array
Set SINK = WScript.CreateObject ("WbemScripting.SWbemSink", "SINK_")
Set objWMIService = GetObject ("winmgmts:" & _
"{impersonationLevel=impersonate}!\" & strComputer & "\ root\ cimv2")
ObjWMIService.ExecNotificationQueryAsync SINK, _
"SELECT * FROM _ _ InstanceCreationEvent WITHIN 1" & _
"WHERE TargetInstance ISA 'Win32_Process'"
Wscript.Echo "Are monitoring processes..."
Do
WScript.Sleep 1000
Loop
'*
Sub SINK_OnObjectReady (objLatestEvent, objAsyncContext)
'Trap asynchronous events.
For Each strTargetProc In arrTargetProcs
If LCase (objLatestEvent.TargetInstance.Name) = LCase (strTargetProc) Then
IntReturn = objLatestEvent.TargetInstance.Terminate
If intReturn = 0 Then
Wscript.Echo "Time:" & Now & ", Succeed!" & chr (9) & _
"Name:" & objLatestEvent.TargetInstance.Name
Else
Wscript.Echo "Time:" & Now & ", Failed!" & chr (9) & _
"Name:" & objLatestEvent.TargetInstance.Name
End If
End If
Next
End Sub
The above is all the contents of the article "how VBScript monitors and ends the specified process". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!