Guest User

Untitled

a guest
Oct 19th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. Option Explicit
  2. Dim Process2Check, Process2Kill
  3. Process2Check = "D:process.bat"
  4. Process2Kill = "D:open.bat"
  5. If AppPrevInstance() Then
  6. MsgBox "Instance already running",VbExclamation,"Instance already running"
  7. WScript.Quit
  8. Else
  9. Do
  10. Call Main(Array(Process2Check))
  11. Call Pause(1)
  12. Loop
  13. End If
  14. '**************************************************************************
  15. Sub Main(colProcessPaths)
  16. Dim ProcessPath
  17. For Each ProcessPath In colProcessPaths
  18. CheckProcess(ProcessPath)
  19. Next
  20. End Sub
  21. '**************************************************************************
  22. Sub CheckProcess(ProcessPath)
  23. Dim ProcessName : ProcessName = StripProcPath(ProcessPath)
  24. With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\.rootcimv2")
  25. With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName))
  26. If .Count = 0 Then
  27. Call Kill(Process2Kill)
  28. Else
  29. Exit Sub
  30. End if
  31. End With
  32. End With
  33. End Sub
  34. '**************************************************************************
  35. Sub Kill(Process2kill)
  36. Dim ProcessName : ProcessName = StripProcPath(Process2kill)
  37. Dim Item,colItems
  38. Set colItems = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\.rootcimv2")_
  39. .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName))
  40. For each Item in colItems
  41. If colItems.Count <> 0 Then
  42. Item.TERMINATE
  43. WScript.Quit
  44. End if
  45. Next
  46. End Sub
  47. '**************************************************************************
  48. Function AppPrevInstance()
  49. With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\.rootcimv2")
  50. With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
  51. " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
  52. AppPrevInstance = (.Count > 1)
  53. End With
  54. End With
  55. End Function
  56. '**************************************************************************
  57. Sub Pause(Sec)
  58. Wscript.Sleep(Sec*1000)
  59. End Sub
  60. '**************************************************************************
  61. Function StripProcPath(ProcessPath)
  62. Dim arrStr : arrStr = Split(ProcessPath, "")
  63. StripProcPath = arrStr(UBound(arrStr))
  64. End Function
  65. '**************************************************************************
  66. Function CommandLineLike(ProcessPath)
  67. ProcessPath = Replace(ProcessPath, "", "\")
  68. CommandLineLike = "'%" & ProcessPath & "%'"
  69. End Function
  70. '**************************************************************************
  71. 'Function to add doubles quotes into a variable
  72. Function DblQuote(Str)
  73. DblQuote = Chr(34) & Str & Chr(34)
  74. End Function
  75. '**************************************************************************
Add Comment
Please, Sign In to add comment