标题: [问题求助] [已解决]VBS进程检测 进程守护 跪求大神帮忙改改 万分感谢 [打印本页]
作者: yapda 时间: 2017-1-13 16:05 标题: [已解决]VBS进程检测 进程守护 跪求大神帮忙改改 万分感谢
本帖最后由 yapda 于 2017-1-13 22:44 编辑
- option Explicit
- dim wmi,proc,procs,proname,flag,WshShell
- Do
- proname="QQ.exe" '需要监测的服务进程的名称,自行替换这里的记事本进程名
- set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
- set procs=wmi.execquery("select * from win32_process")
- flag=true
- for each proc in procs
- if strcomp(proc.name,proname)=0 then
- flag=false
- exit for
- end if
- next
- set wmi=nothing
- if flag then
- Set WshShell = Wscript.CreateObject("Wscript.Shell")
- WshShell.Run ("D:\Tencent\QQ.exe")
- end if
- wscript.sleep 50000 '检测间隔时间,这里是50秒
- loop
复制代码
我只需要尝试启动两次 如果第一次未能启动就结束掉3个进程 然后第二次启动 50秒后检测成功就退出这个vbs 不成功就执行重启命令
作者: yu2n 时间: 2017-1-13 17:41
- Option Explicit
-
- Main
- Sub Main()
- Dim sExeName, sExePath, sDoCmd, sSql
- sExeName = "cmd.exe" 'qq.exe
- sExePath = "C:\windows\system32\cmd.exe" 'd:\qq.exe
- sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe" '刪除3個進程
-
- sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
- sExePath = Replace(sExePath,"\","\\")
- sSql = Replace(sSql,"{P1}",sExeName)
- sSql = Replace(sSql,"{P2}",sExePath)
-
- Dim wim, wso, fso, n
- Set wim = GetObject("winmgmts:")
- Set wso = CreateObject("WScript.Shell")
- Set fso = CreateObject("Scripting.filesystemobject")
-
- Do While n < 2
- If Not fso.FileExists(sExePath) Then Exit Do
- If wim.ExecQuery(sSql).Count = 0 Then
- n = n + 1
- If n = 2 Then
- wso.Run sDoCmd, 1, True
- End If
- wso.Run """" & sExePath & """", 1, False
- End If
- WScript.Sleep 50 * 1000
- Loop
- End Sub
复制代码
作者: yapda 时间: 2017-1-13 18:12
回复 2# yu2n
大神 测试了下 很牛逼 跪谢
但 最后一次检测 无论进程是否存在 代码都自动结束了 没有失败关机这个功能 是我要在后面那里加上关机吗
作者: yu2n 时间: 2017-1-13 19:02
回复 3# yapda - Option Explicit
-
- Main
- Sub Main()
- Dim sExeName, sExePath, sDoCmd, sSql
- sExeName = "cmd.exe" 'qq.exe
- sExePath = "C:\windows\system32\cmd.exe" 'd:\qq.exe
- sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe" '刪除3個進程
-
- sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
- sExePath = Replace(sExePath,"\","\\")
- sSql = Replace(sSql,"{P1}",sExeName)
- sSql = Replace(sSql,"{P2}",sExePath)
-
- Dim wim, wso, fso, n
- Set wim = GetObject("winmgmts:")
- Set wso = CreateObject("WScript.Shell")
- Set fso = CreateObject("Scripting.filesystemobject")
-
- Do While n < 2
- If Not fso.FileExists(sExePath) Then Exit Do
- If wim.ExecQuery(sSql).Count = 0 Then
- n = n + 1
- If n = 2 Then
- wso.Run sDoCmd, 1, True
- End If
- wso.Run """" & sExePath & """", 1, False
- End If
- WScript.Sleep 50 * 1000
- Loop
-
- If wim.ExecQuery(sSql).Count = 0 Then
- wso.Run "shutdown /r /t 10 /f ", 0, False '重启
- End If
- End Sub
复制代码
作者: yapda 时间: 2017-1-13 20:44
回复 4# yu2n
还有一个小问题 大神 就是程序第一次启动成功后 脚本会一直在后台继续检测 能否第一次如果成功也执行 退出脚本操作
作者: yu2n 时间: 2017-1-13 21:07
回复 5# yapda - Option Explicit
-
- Main
- Sub Main()
- Dim sExeName, sExePath, sDoCmd, sSql
- sExeName = "cmd.exe" 'qq.exe
- sExePath = "C:\windows\system32\cmd.exe" 'd:\qq.exe
- sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe" '刪除3個進程
-
- sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
- sExePath = Replace(sExePath,"\","\\")
- sSql = Replace(sSql,"{P1}",sExeName)
- sSql = Replace(sSql,"{P2}",sExePath)
-
- Dim wim, wso, fso, n
- Set wim = GetObject("winmgmts:")
- Set wso = CreateObject("WScript.Shell")
- Set fso = CreateObject("Scripting.filesystemobject")
-
- Do While n < 2
- If Not fso.FileExists(sExePath) Then Exit Do
- If wim.ExecQuery(sSql).Count = 0 Then
- n = n + 1
- If n = 2 Then
- wso.Run sDoCmd, 1, True
- End If
- wso.Run """" & sExePath & """", 1, False
- Else
- Exit Do
- End If
- WScript.Sleep 50 * 1000
- Loop
-
- If wim.ExecQuery(sSql).Count = 0 Then
- wso.Run "shutdown /r /t 10 /f ", 0, False '重启
- End If
- End Sub
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |