欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBScript 监控并结束指定进程的代码

程序员文章站 2022-07-06 12:58:30
运行效果图: 代码(monprocess.vbs): 复制代码 代码如下:on error resume next strcomputer = "."arrtarge...
运行效果图:

VBScript 监控并结束指定进程的代码

代码(monprocess.vbs):

复制代码 代码如下:

on error resume next

strcomputer = "."
arrtargetprocs = array("calc.exe", "notepad.exe", "other.exe")
'数组里为要监视的进程名

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

原文:https://www.enun.net/?p=2385