用vbs实现的瞬间关闭多个系统进程的脚本 |
本文标签:vbs,系统进程 程序试验环境为 windows xp_sp2,主要针对系统存在多个需要中断进程的情况下,瞬间成批中断进程 。 复制代码 代码如下: ---------------------------------------------------------------------------------- On Error Resume next Set fs=CreateObject("scripting.filesystemobject") Set os=CreateObject("wscript.shell") Set os0=createobject("shell.application") Set d0=CreateObject("scripting.dictionary") Set wmi=GetObject("winmgmts:\\.") Set pro_s=wmi.instancesof("win32_process") -------------创建临时文本文件文件,把当前进程输入该文本文件之中并通过记事本打开之 ---------同时把进程对应序号 和 pid 传递给dictionary(d0)一份 filename=fs.GetTempName set f1=fs.CreateTextFile(filename,True) msg="序号"&vbTab&"名称"&vbTab&"PID"&vbTab&"程序文件"&vbtab&now&Chr(10) f1.Writeline(msg) n=1 For Each p In pro_s f1.WriteLine(n&". "&p.name&" , "&p.handle&" , "&p.commandline&Chr(10)) d0.Add ""&n,Trim(p.handle) n=n+1 Next f1.Close os0.MinimizeAll os.Exec "notepad.exe "&filename wscript.sleep 500 --------------等待用户输入欲中断的进程相关的序号列,确定之后关闭并删除临时文本文件 x=InputBox("请根据"&filename&"中的内容"+Chr(10)+ _ "选择需要同时中断的进程对应序号:"+Chr(10)+ _ "(序号之间用,间隔 例如:1,3,5,7,11)","选择") os.AppActivate filename&" - 记事本" os.SendKeys "%fx" WScript.Sleep 500 fs.DeleteFile filename --------如果用户取消了操作,就退出程序 If x="" then wscript.quit --------把用户输入的序号列中相关的序号传递给一个数组 xs xs=Split(x,",",-1,1) -----------对用户输入的序号列进行校对,将重复序号标记为 -2,计算实际序号个数 For i=0 to ubound(xs) ---利用双重循环将重复输入的内容保留一份,其他的标记为-1 for n=0 to ubound(xs) if n=i then n=n+1 if n>ubound(xs) then exit for end if if Trim(xs(n))=Trim(xs(i)) Or _ Trim(xs(n))="" Then xs(n)="-1" end If next Next w=0 ----把不真实可用的序号剔除并计算出其个数 For i=0 To UBound(xs) If d0.Exists(xs(i))=False Then xs(i)="-2" w=w+1 End If Next w=(UBound(xs)+1-w) ---得出可用的序号个数 ------------如果序列中没有输入任何序号就退出程序 If w=0 Then MsgBox "需要中断的进程列表为空!" WScript.Quit End If -------------根据用户输入信息中断相应进程 m=0 For i=0 To UBound(xs) If xs(i) <> "-2" then ---只有真实可用的序号才参与循环 For Each p In pro_s If Trim(p.handle)=trim(d0(xs(i))) Then ---如果进程pid号码正是需要中断的就尝试中断 p_name=p.name pd=p.terminate() If pd=0 Then ---判断中断进程的尝试是否成功 msg=p_name&" 进程中断成功!" m=m+1 Else msg=p_name&" 进程中断失败!" End If os.popup msg,1,"通知",64+0 End If Next end if Next os.popup w&"个目标进程,已经中断了"&m&"个" ,5,"通知",64+0 WScript.quit |