cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
grparsec
Level 7

Start Service after install

Hi,

I am working on a Basic MSI project. I need to start one of the windows services that get installled during my setup, to start running after the installation completed.
I use the following code in my custom action which is after InstallFinalize in the Execute sequence, but my service does not start running. This is the code:
Dim sCmdLine 
Dim Shell

set shell = CreateObject("WScript.Shell")
sCmdLine = "NET START MyService"
shell.Run sCmdLine, 1, True


I am not sure what is wrong here! Can somone please help with this?
Thanks
Labels (1)
0 Kudos
(16) Replies
RobertDickau
Flexera Alumni

Can you use the MSI control-service functionality?

If not, perhaps change your action to run net.exe directly, instead of adding the VBScript layer?
0 Kudos
grparsec
Level 7

RobertDickau wrote:
Can you use the MSI control-service functionality?

If not, perhaps change your action to run net.exe directly, instead of adding the VBScript layer?


Thanks for the reply. I have tried Control NT Services and Install NT Services. I don't have problems installing. But I cannot make starting the service work.
I also tried to copy the 'net.exe' from windows directory to my INSTALLDIR and then have CA to run it to start MyService...it didn't work either.
There should be an easy to this, I just don't know what I am doing wrong:confused: Please help!
0 Kudos
jchristman
Level 8

Have you tried running the VBscript manually to see what it is doing or what the output is. You might need to include the path to NET like so.


Dim sCmdLine
Dim Shell

set shell = CreateObject("WScript.Shell")
sCmdLine = "C:\WINDOWS\system32\NET.exe START MyService"
shell.Run sCmdLine, 1, True


If this does not work try creating a batch file "StartMyService.bat" that you can execute after the installation.
0 Kudos
RobertDickau
Flexera Alumni

(As a big aside, it's generally better to avoid VBScript custom actions that touch the target system, esp. since spyware scanners and such often stop VBScript code from running.

It's also generally better to avoid custom actions to duplicate built-in MSI functionality, since built-in functionality handles rollback and so forth. In what way is the Control NT Service functionality not working?)
0 Kudos
TheTraveler
Level 8

Here is a question, can you use "ServiceStartService" in a MSI project? I know you can use it in an Install Shield Script project without any problems.
0 Kudos
grparsec
Level 7

jchristman wrote:
Have you tried running the VBscript manually to see what it is doing or what the output is. You might need to include the path to NET like so.


Dim sCmdLine
Dim Shell

set shell = CreateObject("WScript.Shell")
sCmdLine = "C:\WINDOWS\system32\NET.exe START MyService"
shell.Run sCmdLine, 1, True


If this does not work try creating a batch file "StartMyService.bat" that you can execute after the installation.

Thanks for the reply. The Script works from outside the MSI. Also I tried to include the whole path to NET..again works from outside the MSI but not within.
0 Kudos
Christopher_Pai
Level 16

Yes, you can. However, I think your time would be better served getting the Built-IN MSI ServiceControl pattern. It's simpler and more reliable. The only times you should need a custom action ( in any language ) to start a service is situations where the service has a start dependency that's not yet available ( for example, a DLL in the GAC that isn't published until the commit phase ).

Associate a Service Control action to a component that is being installed, join it to the service using the `internal` service name ( like w3svc ) and set the action flags for what makes sense to you. ( uninstall: stop, install: stop,start ).
0 Kudos
grparsec
Level 7

RobertDickau wrote:
(As a big aside, it's generally better to avoid VBScript custom actions that touch the target system, esp. since spyware scanners and such often stop VBScript code from running.

It's also generally better to avoid custom actions to duplicate built-in MSI functionality, since built-in functionality handles rollback and so forth. In what way is the Control NT Service functionality not working?)


Thanks a lot Robert for the interesting hints. My problem with using Control NT AService is that I get the following error:
Error 1920: Service MyService(MyService) failed to start. Verufy that you have sufficient privileges to start system services.
0 Kudos
grparsec
Level 7

TheTraveler wrote:
Here is a question, can you use "ServiceStartService" in a MSI project? I know you can use it in an Install Shield Script project without any problems.


I am not sure about that!
0 Kudos
jchristman
Level 8

Are you requiring the user to be an administrator.
0 Kudos
TheTraveler
Level 8

Hello,

I think I know why it doesn't work within the MSI project. You see, when you run it in a batch file, it uses the system enviroment to run the commands you are issuing. Try this,

"WINSYSDIR\cmd.exe" /c "C:\WINDOWS\system32\NET.exe" START MyService


You might have to play around with it, but this might work. You see, when you run cmd, it loads in the OS's environment and allows you to run the command of your choice in that environment when you use the "/c" parameter. I hope this helps.
0 Kudos
Christopher_Pai
Level 16

I need a lot more information to troubleshoot that one:

What OS are you installing on?
If Vista:
Are you using setup.exe? What's it's Required Execution Level set to?
What is the Summary Information Stream Require Admin Priv set to?

For ALL OS:
What account are you installing as?
Is that account a member of the Local Administrators group?
Are you doing a Per-User or Per-Machine Install?
What service are you starting?
What account does the service use to run?
0 Kudos
jchristman
Level 8

Here give this a try i have used it a couple of times with a few minor changes, it is not mine I got off the net from www.activexperts.com


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='NetDDE'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='NetDDE'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
objService.StartService()
Next

0 Kudos
Christopher_Pai
Level 16

I prefer this simpler, reliable and elegant solution. If for some reason there is an absolutely valid reason that MSI can't natively handle your particular scenario, the InstallScript ServiceStartService() function is much cleaner and less fragile method of starting the service without putting dependencies on ActiveScript and WMI ( which often fail ).
0 Kudos
grparsec
Level 7

Thank you everybody for your hints ans supports. I was truely amazed by all the replys that I got on this post. Thank you so very much.

I was finally able to start the service after the install and stop & delete it on uninstall using 'Control NT Services'. It works like a charm on both Win 2003 and Win 2008 servers. I guess the way I was using it before was not proper (I was using 'Install NT Services' and 'Control NT Services' withing the same component and that was I guess why I was getting error. After I separated these two, my service installs and runs fine).

At this point I don't know how to uninstall and reinstall the service during 'Maintenance' or 'Patch'. I've seen that people duplicate the Installutil.exe from windows to their project and use a CA to run it to uninstall their service during the maintennace. I do not want to use the same strategy...what do you think is the best way to do this?

I welcome all your ideas.
Thanks again.
0 Kudos
Christopher_Pai
Level 16

Correct, from a Windows Installer perspective, the ServiceController class invoked by InstallUtil is an antipattern. See: http://blog.deploymentengineering.com/2006/07/msi-vs-net.html

If you look at the picture in the post above... keep in mind that `Install` and `Uninstall` is a bit of a misnomer. It's not equivilant to `Not Installed` and `REMOVE=ALL`, it's equivilant the the component action state that it's associated to ( &Component=3 or &Component=2 ). So from that perspective, you probably want to use the Install Delete and Install Stop attributes since in a patch/upgrade scenario the component is being reinstalled and the existing service will possibly need to be stopped and be deleted then recreated and restarted.
0 Kudos