C# 使用给定的用户名在远程机器上安装 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1159012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:37:09  来源:igfitidea点击:

Installing a windows service on remote machine using given username

c#.netmsbuildwindows-services

提问by Grzenio

What is the best way to install a windows service written in C# (in the standard way) on a remote machine, where I need to provide the username and password it should run as?

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

I am going to run it from MSBuild as part of integration tests.

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

EDIT: I don't have an msi and I don't want to create one.

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

采纳答案by Patrick McDonald

You can use the SCcommand.

您可以使用SC命令。

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

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

(Note the spaces after the equals signs are important)

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

Creates a service entry in the registry and Service Database.
SYNTAX: 
sc create [service name] [binPath= ] <option1> <option2>...
CREATE OPTIONS:
NOTE: The option name includes the equal sign.
 type= <own|share|interact|kernel|filesys|rec>
       (default = own)
 start= <boot|system|auto|demand|disabled>
       (default = demand)
 error= <normal|severe|critical|ignore>
       (default = normal)
 binPath= <BinaryPathName>
 group= <LoadOrderGroup>
 tag= <yes|no>
 depend= <Dependencies(separated by / (forward slash))>
 obj= <AccountName|ObjectName>
       (default = LocalSystem)
 DisplayName= <display name>
 password= <password> 

回答by Raj More

Installutil called from WMI invoked from Powershell is one way to go.

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

回答by Mike Chaliy

We used to use PsExecto do everything on remote machine. At this time I saw naother solution called PowerShell Remoting, but haven't tried myself.

我们过去常常使用PsExec在远程机器上做所有事情。此时我看到了名为PowerShell Remoting 的naother 解决方案,但我自己还没有尝试过。

回答by Echilon

It might be worth you checking out a utility I wrote which lets you install/uninstall/start/stop windows services on a local or remote machine. If you have a feature you need added let me know (comment/github issues or PM and I'll happily oblige).

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

ServiceConfigurator on GitHub

GitHub 上的服务配置器

As @Raj More suggested, it uses WMI invoked from .NET.

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