[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] [已解决]VBS进程检测 进程守护 跪求大神帮忙改改 万分感谢

本帖最后由 yapda 于 2017-1-13 22:44 编辑
  1. option Explicit   
  2. dim wmi,proc,procs,proname,flag,WshShell   
  3. Do  
  4.     proname="QQ.exe" '需要监测的服务进程的名称,自行替换这里的记事本进程名   
  5. set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")   
  6. set procs=wmi.execquery("select * from win32_process")   
  7.   flag=true   
  8. for each proc in procs   
  9.     if strcomp(proc.name,proname)=0 then   
  10.       flag=false   
  11.       exit for   
  12.     end if   
  13. next   
  14.   set wmi=nothing   
  15.   if flag then   
  16.     Set WshShell = Wscript.CreateObject("Wscript.Shell")   
  17.     WshShell.Run ("D:\Tencent\QQ.exe")   
  18. end if   
  19.   wscript.sleep 50000 '检测间隔时间,这里是50秒   
  20. loop  
复制代码
我只需要尝试启动两次  如果第一次未能启动就结束掉3个进程 然后第二次启动    50秒后检测成功就退出这个vbs    不成功就执行重启命令
1

评分人数

  1. Option Explicit
  2. Main
  3. Sub Main()
  4.     Dim sExeName, sExePath, sDoCmd, sSql
  5.     sExeName = "cmd.exe"    'qq.exe
  6.     sExePath = "C:\windows\system32\cmd.exe"    'd:\qq.exe
  7.     sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe"    '刪除3個進程
  8.     sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
  9.     sExePath = Replace(sExePath,"\","\\")
  10.     sSql = Replace(sSql,"{P1}",sExeName)
  11.     sSql = Replace(sSql,"{P2}",sExePath)
  12.     Dim wim, wso, fso, n
  13.     Set wim = GetObject("winmgmts:")
  14.     Set wso = CreateObject("WScript.Shell")
  15.     Set fso = CreateObject("Scripting.filesystemobject")
  16.     Do While n < 2
  17.         If Not fso.FileExists(sExePath) Then Exit Do
  18.         If wim.ExecQuery(sSql).Count = 0 Then
  19.             n = n + 1
  20.             If n = 2 Then
  21.                 wso.Run sDoCmd, 1, True
  22.             End If
  23.             wso.Run """" & sExePath & """", 1, False
  24.         End If
  25.         WScript.Sleep 50 * 1000
  26.     Loop
  27. End Sub
复制代码
1

评分人数

    • yapda: 就是厉害的技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 2# yu2n


    大神  测试了下  很牛逼  跪谢         

     但 最后一次检测   无论进程是否存在   代码都自动结束了    没有失败关机这个功能   是我要在后面那里加上关机吗

TOP

回复 3# yapda
  1. Option Explicit
  2. Main
  3. Sub Main()
  4.     Dim sExeName, sExePath, sDoCmd, sSql
  5.     sExeName = "cmd.exe"    'qq.exe
  6.     sExePath = "C:\windows\system32\cmd.exe"    'd:\qq.exe
  7.     sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe"    '刪除3個進程
  8.     sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
  9.     sExePath = Replace(sExePath,"\","\\")
  10.     sSql = Replace(sSql,"{P1}",sExeName)
  11.     sSql = Replace(sSql,"{P2}",sExePath)
  12.     Dim wim, wso, fso, n
  13.     Set wim = GetObject("winmgmts:")
  14.     Set wso = CreateObject("WScript.Shell")
  15.     Set fso = CreateObject("Scripting.filesystemobject")
  16.     Do While n < 2
  17.         If Not fso.FileExists(sExePath) Then Exit Do
  18.         If wim.ExecQuery(sSql).Count = 0 Then
  19.             n = n + 1
  20.             If n = 2 Then
  21.                 wso.Run sDoCmd, 1, True
  22.             End If
  23.             wso.Run """" & sExePath & """", 1, False
  24.         End If
  25.         WScript.Sleep 50 * 1000
  26.     Loop
  27.     If wim.ExecQuery(sSql).Count = 0 Then
  28.         wso.Run "shutdown /r /t 10 /f ", 0, False    '重启
  29.     End If
  30. End Sub
复制代码
1

评分人数

    • yapda: 完美 解决技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 4# yu2n


    还有一个小问题   大神    就是程序第一次启动成功后   脚本会一直在后台继续检测    能否第一次如果成功也执行 退出脚本操作

TOP

回复 5# yapda
  1. Option Explicit
  2. Main
  3. Sub Main()
  4.     Dim sExeName, sExePath, sDoCmd, sSql
  5.     sExeName = "cmd.exe"    'qq.exe
  6.     sExePath = "C:\windows\system32\cmd.exe"    'd:\qq.exe
  7.     sDoCmd = "taskkill /f /im 1.exe /im 2.exe /im 3.exe"    '刪除3個進程
  8.     sSql = "Select * From Win32_Process Where Name='{P1}' And CommandLine Like '%{P2}%'"
  9.     sExePath = Replace(sExePath,"\","\\")
  10.     sSql = Replace(sSql,"{P1}",sExeName)
  11.     sSql = Replace(sSql,"{P2}",sExePath)
  12.     Dim wim, wso, fso, n
  13.     Set wim = GetObject("winmgmts:")
  14.     Set wso = CreateObject("WScript.Shell")
  15.     Set fso = CreateObject("Scripting.filesystemobject")
  16.     Do While n < 2
  17.         If Not fso.FileExists(sExePath) Then Exit Do
  18.         If wim.ExecQuery(sSql).Count = 0 Then
  19.             n = n + 1
  20.             If n = 2 Then
  21.                 wso.Run sDoCmd, 1, True
  22.             End If
  23.             wso.Run """" & sExePath & """", 1, False
  24.         Else
  25.             Exit Do
  26.         End If
  27.         WScript.Sleep 50 * 1000
  28.     Loop
  29.     If wim.ExecQuery(sSql).Count = 0 Then
  30.         wso.Run "shutdown /r /t 10 /f ", 0, False    '重启
  31.     End If
  32. End Sub
复制代码
1

评分人数

    • yapda: 万分感谢 大神的耐心技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表