如何从命令行指定名称和描述安装Windows服务?

LaB*_*cca 12 windows-services shellexecute

我用Delphi为客户端服务器应用程序创建了一个Windows服务.

要安装它,我使用

c:\Test\MyService.exe /install (or /uninstall)
Run Code Online (Sandbox Code Playgroud)

这将安装该服务,并在Windows服务中列出"MyService"名称和空描述.

如何定义不同的名称并插入描述(在运行时可以看到services.msc)?

注意:我需要这个,因为在同一台机器上我需要安装更多次相同的服务(每个数据库1个).

目前我唯一的解决方法是重命名服务exe,但我更愿意找到正确的命令行方式(因为我这样做ShellExecute).

更新:不知怎的,我会寻找类似的东西(这当然只是出于解释原因! - InstallService.exe是我刚刚发明的名字):

InstallService.exe c:\Test\MyService.exe /install /name='MyService1' 
  /description='This is my service for database 1'
Run Code Online (Sandbox Code Playgroud)

而且更紧凑的版本会很好:

c:\Test\MyService.exe /install /name='MyService1' 
  /description='This is my service for database 1'
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 13

Windows已经附带了您需要的实用程序,即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= 

这将创建服务并允许您指定名称和显示名称.

要修改您需要的描述sc description:

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

另一个明显的选择是在您的服务中构建命令行解析.这很容易做到.只需为服务BeforeInstall和/或AfterInstall事件分配处理程序,然后在那里处理交换机.

  • 容易错过的东西(即使它在sc的描述中)是你需要binPath =之后的空格,所以你不能做binPath = mypath,而你必须做binPath = mypath (3认同)
  • 而且`sc delete [service name]`关闭循环 (2认同)
  • **示例:**[Microsoft知识库](http://support.microsoft.com/kb/251192) - [创建,查询,停止和删除示例](http://www.herongyang.com/Windows /Service-Create-Delete-Services-with-sc-exe.html) - [关于参数的讨论](http://stackoverflow.com/questions/3663331/creating-a-service-with-sc-exe-how -to-通在上下文参数) (2认同)