Created
February 2, 2016 17:40
-
-
Save mreschke/60f912e89de6b9625cad to your computer and use it in GitHub Desktop.
Will start/stop a windows processes, only if not running
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
'mReschke fo zabbix 2009-03-12 | |
'Will start/stop a process | |
'Will only start 1 process, will not start if already started, no multiples | |
Dim ArgObj, strComputer, objWMIService, colProcesses, Process | |
Dim processPath, processName, processAction | |
Set ArgObj = WScript.Arguments | |
processPath = ArgObj(0) 'Command parameter: full path (no filename) to executable (with \ at end) | |
processName = ArgObj(1) 'Command parameter: file name (no fullpath) of executable | |
processAction = ArgObj(2) 'Command parameters (start or stop) | |
'Example Script.bat (note I path to the .exe dir so it runs in its locat folder) | |
'd: | |
'cd \DynaMailGenerator\bin | |
'd:\zabbix\scripts\StartProc.vbs d:\DynaMailGenerator\bin\ DynaMailGeneratorTEST.exe start | |
If IsRunning = True Then | |
If processAction = "stop" Then | |
Call KillProcess | |
End IF | |
Else | |
If processAction = "start" then | |
Call StartProcess | |
End If | |
End If | |
Function IsRunning() | |
Dim processRunning | |
processRunning = false | |
strComputer = "." | |
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") | |
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'") | |
For Each Process in colProcesses | |
If Process.Name = processName Then | |
processRunning = True | |
End If | |
Next | |
IsRunning = processRunning 'return processRunning (true or false) | |
End Function | |
Sub StartProcess() | |
'Start the Process | |
Dim oShell | |
Set oShell = WScript.CreateObject ("WSCript.shell") | |
oShell.run "" & processPath & processName & "" | |
Set oShell = Nothing | |
End Sub | |
Sub KillProcess() | |
strComputer = "." | |
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") | |
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'") | |
For Each Process in colProcesses | |
If Process.Name = processName Then | |
Process.Terminate() | |
End If | |
next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment