1

In VBScript is it possible to query a specific service name if it's started or running? if its running then execute finally my application. So here is how the flow i am trying to prepare:

  1. System is booted
  2. Windows 8.1 Startup script is executed (VBS)
  3. VBScript now wait and keep checking if the service name "NGINX" started or running already.

    1. If RUNNING, then execute notepad.exe.
    2. If not RUNNING, then start the service "NGINX" and make sure its now "RUNNING". if now "RUNNING" then finally execute notepad.exe.

Is this possible with VBScript?

Example: this line 6 should be only executed if the service NGINX is running otherwise it should never execute that.

Option Explicit
Dim ws
set ws = CreateObject("wscript.shell")
Dim a
a = "C:\Program Files (x86)\abcd\kiosk.jar"
ws.run "java.exe -cp " & chr(34) & a & chr(34) & " kiosk", 0, False
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 2
    [Check to See if a Service is Installed and Running](https://gallery.technet.microsoft.com/scriptcenter/01fcf945-ad73-44e0-8cb5-152432bc6bcf) – JosefZ Jun 18 '15 at 08:17

2 Answers2

0

If you want nginx automatically started after system boot you should install it as a service. You can use a WMI query against the Win32_Service class to check if a service is running, and also start it in case it isn't:

Set wmi = GetObject("winmgmts.//./root/cimv2")

qry = "SELECT * FROM Win32_Service WHERE Name = 'nginx' AND State <> 'Running'"
For Each svc In wmi.ExecQuery(qry)
  svc.StartService
Next

To just check if the service is running and run some other program if it is you could do something like this:

isRunning = False
qry = "SELECT * FROM Win32_Service WHERE Name = 'nginx' AND State = 'Running'"
For Each svc In wmi.ExecQuery(qry)
  isRunning = True
Next

If isRunning Then
  CreateObject("WScript.Shell").Run "notepad.exe", 0
End If
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

No need to install it as a service. Use a scheduled task to launch it at start up, login, or whatever. I like the WMI method and that would be what I'd want but you could also execute Net Start, read each line of output, then use If Instr to check for your service name. If it comes back True then your service is started. If not, then it hasn't. Nice thing is that you'd already have your connection created to your Wscript.shell so it would be a simple matter to use Net Start again to start the service. http://www.codeproject.com/Tips/507798/Differences-between-Run-and-Exec-VBScript