gallyjh
Posts: 27
Joined: Tue Dec 20, 2011 5:01 pm

Starting a Windows service via VBscript

Tue Aug 12, 2014 6:59 pm

Hi,

I silently install MongoDB, and configure the database successfully via my vbs script. I can tell from going into Windows services that the MongoDB service is installed properly. I just need to start it. In my script, I attempt to start the service with the following:

Code: Select all

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'MongoDB'")
For Each objService in colListOfServices
    objService.StartService()
Next
I've tested the code in an elevated command prompt running "wscript startservice.vbs" and it works just fine. However when it's ran via the installer the service is not running. Here is the order of custom actions:
2014-08-12_10h57_50.png
2014-08-12_10h57_50.png (14.54KiB)Viewed 5513 times
Any idea why this won't work? Is there a better practice to this?

Thanks!

gallyjh
Posts: 27
Joined: Tue Dec 20, 2011 5:01 pm

Re: Starting a Windows service via VBscript

Tue Aug 12, 2014 8:06 pm

OK seems like I solved the issue. For future reference for others, here is how I went about it:

I decided to remove the function from the VBS script. Instead I went with a batch file like:

Code: Select all

@ECHO OFF
SC START MONGODB
I then just added the Launch File custom action like so:
2014-08-12_12h05_18.png
2014-08-12_12h05_18.png (94.91KiB)Viewed 5511 times

gallyjh
Posts: 27
Joined: Tue Dec 20, 2011 5:01 pm

Re: Starting a Windows service via VBscript

Tue Aug 12, 2014 9:12 pm

OK, nevermind, once I removed some 'PAUSE' statements it stopped working again. I'm not sure what's going on here. So I threw the previous statements back into VBS, and added MsgBox just before the statements where it starts the service, and guess what? It worked! Once I removed the MsgBox, it stopped working. So it had me believing that there was a timing issue. So I threw in a Wscript.Sleep 30000, for it to sleep 30 seconds before attempting to start the service. Still not working. I'm lost here...

It seems like making the script or batch file interactive makes it work, clearly this isn't a viable option though.

Any idea?

gallyjh
Posts: 27
Joined: Tue Dec 20, 2011 5:01 pm

Re: Starting a Windows service via VBscript

Tue Aug 12, 2014 9:26 pm

Alright, hopefully last follow-up. I replaced the Wscript.Sleep 30000 with:

Code: Select all

Dim dteWait
dteWait = DateAdd("s", 10, Now())
Do Until (Now() > dteWait)
Loop
and I have no MsgBox's for interaction. This seems to be working...

Eusebiu
Posts: 4931
Joined: Wed Nov 14, 2012 2:04 pm

Re: Starting a Windows service via VBscript

Wed Aug 13, 2014 7:48 am

Hi,

I'm glad you got this working.

Also, please keep in mind that in order to configure a service you can use the predefined support available in the Services page of an Advanced Installer project. If you only want to start it, you can add a control operation for that service.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

gallyjh
Posts: 27
Joined: Tue Dec 20, 2011 5:01 pm

Re: Starting a Windows service via VBscript

Wed Aug 13, 2014 7:09 pm

Hi Eusebiu,

I think I did try this route as well. But I think there was an issue with timing and sequence. For example I needed the MongoDB service to be started for my Application Service to be started, and it doesn't seem you can choose your sequence.

Eusebiu
Posts: 4931
Joined: Wed Nov 14, 2012 2:04 pm

Re: Starting a Windows service via VBscript

Thu Aug 14, 2014 9:11 am

Hi,

You can take a look on the Set an order for services to be started thread where is discussed about how services can be ordered.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”