Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a windows service from command line specifying name and description?

I created a Windows service with Delphi for a client server application.

To install it I use

c:\Test\MyService.exe /install (or /uninstall)

This installs the service and in Windows services it lists with "MyService" name and empty description.

How to define a different name and insert a description (to be seen when running services.msc)?

Note: I need this because on the same machine i need to install more times the same service (1 per database).

Currently the only workaround i foudn is to rename the service exe, but I'd prefer to find out the correct command line way to do it (since I do this from ShellExecute).

Update: Somehow i'd look for something like (this is just for explanation reasons of course! - InstallService.exe is a name i just invented):

InstallService.exe c:\Test\MyService.exe /install /name='MyService1' 
  /description='This is my service for database 1'

but also a more compact version would be fine like:

c:\Test\MyService.exe /install /name='MyService1' 
  /description='This is my service for database 1'
like image 697
LaBracca Avatar asked Aug 29 '13 10:08

LaBracca


People also ask

How do I create a Windows service from the command-line?

The SC Create command uses the following format: sc create serviceName binpath= "path\to\service-wrapper-7.4.exe" optionName= optionValue ... where: create is the command to be run by SC (this command name is mandatory to create a service).

How do I install the same Windows service with a different name?

In case you need the same service running on the same host but with different configuration, logically you would use same code just copy the service folder with different configuration and use installutil to install service with a different name.

What command-line CMD command will show service information?

Run the sc query command. The sc query command queries all Windows services and returns information such as the name ( SERVICE_NAME ), display name ( DISPLAY_NAME ), state, and more. If you don't know the service name, sc query is your friend to find it. 3.


1 Answers

Windows already ships with the utility that you need, namely sc create.

>sc create /?
DESCRIPTION:
        Creates a service entry in the registry and Service Database.
USAGE:
        sc  create [service name] [binPath= ]  ...

OPTIONS:
NOTE: The option name includes the equal sign.
      A space is required between the equal sign and the value.
 type= 
       (default = own)
 start= 
       (default = demand)
 error= 
       (default = normal)
 binPath= 
 group= 
 tag= 
 depend= 
 obj= 
       (default = LocalSystem)
 DisplayName= 
 password= 

This will create the service and allow you to specify the name and display name.

To modify the description you need sc description:

>sc description /?
DESCRIPTION:
        Sets the description string for a service.
USAGE:
        sc  description [service name] [description]

The other obvious option is to build command line parsing into your service. That's trivially easy to do. Simply assign handlers for the service's BeforeInstall and/or AfterInstall events and process the switches there.

like image 114
David Heffernan Avatar answered Oct 30 '22 16:10

David Heffernan