Board logo

标题: [问题求助] 【已解决】vbs检测进程 [打印本页]

作者: yyz219    时间: 2022-9-16 11:24     标题: 【已解决】vbs检测进程

本帖最后由 yyz219 于 2022-9-17 09:55 编辑

问题解决了:
  1. '【有notepad2.exe进程--→运行"用Notepad3打开.vbs"】【无notepad2.exe进程--→运行"用Notepad2打开.vbs"】
  2. ProcExist = "0"
  3. Set objWMIProcess = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
  4. For Each objProcess In objWMIProcess
  5. If objProcess.Name = "Notepad2.exe" Then  '【左边是需要检测的进程】
  6. ProcExist = "1"
  7. '【notepad2.exe进程存在】【用notepad3.exe打开文本】
  8. CreateObject("WScript.Shell").Run "用Notepad3打开.vbs",0
  9. WScript.Quit '【退出】
  10. End If
  11. Next
  12. If ProcExist = "0" Then
  13. '【notepad2.exe进程不存在】【用Notepad2打开打开文本】
  14. CreateObject("WScript.Shell").Run "用Notepad2打开.vbs",0
  15. WScript.Quit '【退出】
  16. End If
复制代码
*************************************************************
原来的问题:
00.vbs:

If IsProcess("qq.exe") =True Then

MessageBox"发现进程"

Else

MessageBox"没有发现进程"

End If


作者: Batcher    时间: 2022-9-16 12:27

回复 1# yyz219
  1. Set objWMIProcess = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
  2. For Each objProcess In objWMIProcess
  3.     If objProcess.Name = "QQ.exe" Then
  4.         WScript.Echo objProcess.Name
  5.     End If
  6. Next
复制代码

作者: yyz219    时间: 2022-9-16 12:37

回复  yyz219
Batcher 发表于 2022-9-16 12:27



    能不能弄出:MessageBox"发现进程" 和  MessageBox"没有发现进程?

谢谢
作者: Batcher    时间: 2022-9-16 14:05

回复 3# yyz219
  1. ProcExist = "0"
  2. Set objWMIProcess = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
  3. For Each objProcess In objWMIProcess
  4.     If objProcess.Name = "QQ.exe" Then
  5.         ProcExist = "1"
  6.     End If
  7. Next
  8. If ProcExist = "0" Then
  9.     MsgBox "Process QQ.exe not exist"
  10. Else
  11.     MsgBox "Process QQ.exe exist"
  12. End If
复制代码

作者: Batcher    时间: 2022-9-16 14:06

如果进程不存在则启动相关应用程序
  1. ProcExist = "0"
  2. Set objWMIProcess = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
  3. For Each objProcess In objWMIProcess
  4.     If objProcess.Name = "QQ.exe" Then
  5.         ProcExist = "1"
  6.     End If
  7. Next
  8. If ProcExist = "0" Then
  9.     Set objShell = WScript.CreateObject ("WSCript.Shell")
  10.     objShell.Run """C:\Program Files (x86)\Tencent\QQ\Bin\QQ.exe"""
  11. End If
复制代码

作者: yyz219    时间: 2022-9-16 14:17

如果进程不存在则启动相关应用程序
Batcher 发表于 2022-9-16 14:06



    非常感谢
作者: yyz219    时间: 2022-9-16 15:58

本帖最后由 yyz219 于 2022-9-16 17:03 编辑

可以了(就是代码还是有一点长)【不知道还能不能精简】:

ProcExist = "0"
Set objWMIProcess = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
For Each objProcess In objWMIProcess
    If objProcess.Name = "Notepad2.exe" Then
        ProcExist = "1"
MsgBox "发现进程"
WScript.Quit
   End If
Next
If ProcExist = "0" Then
    MsgBox "没有发现进程"
WScript.Quit
End If




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2