使用给定的用户名在远程计算机上安装Windows服务

在远程计算机上安装用C#编写的Windows服务(以标准方式)的最佳方法是什么,我需要提供应该运行的用户名和密码?

我将从MSBuild运行它作为集成测试的一部分。

编辑:我没有msi,我不想创建一个。

您可以使用SC命令。

sc.exe \\remotecomputer create newservice binpath= C:\Windows\System32\Newserv.exe start= auto obj= DOMAIN\username password= pwd

(注意等号后的空格很重要)

 Creates a service entry in the registry and Service Database. SYNTAX: sc create [service name] [binPath= ]  ... CREATE OPTIONS: NOTE: The option name includes the equal sign. type=  (default = own) start=  (default = demand) error=  (default = normal) binPath=  group=  tag=  depend=  obj=  (default = LocalSystem) DisplayName=  password=  

从Powershell调用的WMI调用的Installutil是一种方法。

我们曾经使用PsExec来做远程机器上的所有事情。 这时我看到了另一个名为PowerShell Remoting的解决方案,但还没试过自己。

您可能值得查看我编写的实用程序,该实用程序允许您在本地或远程计算机上安装/卸载/启动/停止Windows服务。 如果你有一个function,你需要添加让我知道(评论/ github问题或PM,我很乐意)。

GitHub上的ServiceConfigurator

正如@Raj More所建议的,它使用从.NET调用的WMI。