如何在安装而不是每次启动时一次性传递参数给Windows服务

我们有一个Windows服务应用程序,可以接受命令行参数,如:

MyService -option 

到目前为止,当我们想用参数启动服务的时候,我们可以从服务属性对话框(在开始参数框中)或者通过命令

 sc start MyService -option 

我们想要的是通过这个参数“永久”安装服务的方法,这样用户就可以不必每次都设置参数就可以启动/停止服务

顺便说一句,在ImagePathregistry项中添加参数不起作用,也不会像这样安装:

 MyService -option /install 

更新 :谢谢你迄今为止的答案,这有助于我改进问题。
我想实现的是在服务级别本身设置参数(如使用属性),以防在同一个可执行文件中有多个服务。 binpathconfiguration选项只是更新registry中的ImagePath条目。 这不能是服务特定的。

 sc config MyService binPath= MyService.exe -option 

更新

单个服务参数存储在注册表中的HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters 。 我不确定参数是如何传递给服务的。 我相信 SCM读取这些值,然后当它调用StartService时,它将它们传递给ServiceMain回调。

把参数放在配置文件里怎么样?

通过ImagePath在命令行上传递的参数可以在main()或GetCommandLine()中访问。 你可以用命令行参数安装,然后在你的ServiceMain中,检查是否有任何参数在lpszArgs参数中传递。 如果没有,请调用GetCommandLine并查看是否有任何通过的方式。

Powershell可以做到这一点,但你必须使用.Net来实现它。

 new-Object System.ServiceProcess.ServiceController("$ServiceName",$ComputerName)).Start("$Parameter") 

根据ServiceBase.OnStart文档:

在控制台中输入的参数不会被保存。 当服务从控制面板启动时,它们被一次性传递给服务。 在服务自动启动时必须存在的参数可以放在服务注册表项(HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \)的ImagePath字符串值中。 您可以使用GetCommandLineArgs方法从注册表中获取参数,例如:string [] imagePathArgs = Environment.GetCommandLineArgs();.

如果有多个服务具有相同的可执行文件,那么您将使用不同的服务名称来安装它们。 您可以参考服务名称而不是参数。

要获取服务名称,您可以使用此方法Windows服务如何确定其ServiceName?

使用SC(服务控制)命令,它给了你更多的选择,而不仅仅是启动和停止。

 DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc <server> [command] [service name] ... The option <server> has the form "\\serverName" Further, help on commands can be obtained by typing: "sc [command]" Commands: query-----------Queries the status for a service, or enumerates the status for types of services. queryex---------Queries the extended status for a service, or enumerates the status for types of services. start-----------Starts a service. pause-----------Sends a PAUSE control request to a service. interrogate-----Sends an INTERROGATE control request to a service. continue--------Sends a CONTINUE control request to a service. stop------------Sends a STOP request to a service. config----------Changes the configuration of a service (persistent). description-----Changes the description of a service. failure---------Changes the actions taken by a service upon failure. qc--------------Queries the configuration information for a service. qdescription----Queries the description for a service. qfailure--------Queries the actions taken by a service upon failure. delete----------Deletes a service (from the registry). create----------Creates a service. (adds it to the registry). control---------Sends a control to a service. sdshow----------Displays a service's security descriptor. sdset-----------Sets a service's security descriptor. GetDisplayName--Gets the DisplayName for a service. GetKeyName------Gets the ServiceKeyName for a service. EnumDepend------Enumerates Service Dependencies. The following commands don't require a service name: sc <server> <command> <option> boot------------(ok | bad) Indicates whether the last boot should be saved as the last-known-good boot configuration Lock------------Locks the Service Database QueryLock-------Queries the LockStatus for the SCManager Database EXAMPLE: sc start MyService 

如果您想在您的服务自动启动时传递参数,这可能有所帮助: https : //stackoverflow.com/a/41319479/4521055