.net 使用 InstallUtil 并静默设置 Windows 服务登录用户名/密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/140054/
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
Using InstallUtil and silently setting a windows service logon username/password
提问by Dean Hill
I need to use InstallUtil to install a C# windows service. I need to set the service logon credentials (username and password). All of this needs to be done silently.
我需要使用 InstallUtil 来安装 C# windows 服务。我需要设置服务登录凭据(用户名和密码)。所有这些都需要默默地完成。
Is there are way to do something like this:
有没有办法做这样的事情:
installutil.exe myservice.exe /customarg1=username /customarg2=password
回答by Jimbo
A much easier way than the posts above and with no extra code in your installer is to use the following:
比上面的帖子更简单且安装程序中没有额外代码的方法是使用以下内容:
installUtil.exe /username=domain\username /password=password /unattended C:\My.exe
installUtil.exe /username=domain\username /password=password /unattended C:\My.exe
Just ensure the account you use is valid. If not you will receive a "No mapping between account names and security id's was done" exception
只要确保您使用的帐户是有效的。如果不是,您将收到“未完成帐户名称和安全 ID 之间的映射”异常
回答by Dean Hill
Bravo to my co-worker (Bruce Eddy). He found a way we can make this command-line call:
向我的同事 (Bruce Eddy) 致敬。他找到了一种我们可以进行命令行调用的方法:
installutil.exe /user=uname /password=pw myservice.exe
It is done by overriding OnBeforeInstall in the installer class:
它是通过覆盖安装程序类中的 OnBeforeInstall 来完成的:
namespace Test
{
[RunInstaller(true)]
public class TestInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller serviceProcessInstaller;
public OregonDatabaseWinServiceInstaller()
{
serviceInstaller = new ServiceInstaller();
serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Test";
serviceInstaller.DisplayName = "Test Service";
serviceInstaller.Description = "Test";
serviceInstaller.StartType = ServiceStartMode.Automatic;
Installers.Add(serviceInstaller);
serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = ServiceAccount.User;
Installers.Add(serviceProcessInstaller);
}
public string GetContextParameter(string key)
{
string sValue = "";
try
{
sValue = this.Context.Parameters[key].ToString();
}
catch
{
sValue = "";
}
return sValue;
}
// Override the 'OnBeforeInstall' method.
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
string username = GetContextParameter("user").Trim();
string password = GetContextParameter("password").Trim();
if (username != "")
serviceProcessInstaller.Username = username;
if (password != "")
serviceProcessInstaller.Password = password;
}
}
}
回答by Josua
InstallUtil.exesets StartupType=Manual
InstallUtil.exe设置 StartupType=Manual
In case you want to autostart the service, use:
如果要自动启动服务,请使用:
sc config MyServiceName start= auto
sc config MyServiceName start= auto
(note there there has to be a space after '=')
(注意'='后必须有一个空格)
回答by blowdart
No, installutil doesn't support that.
不, installutil 不支持。
Of course if you wrote an installer; with a custom actionthen you would be able to use that as part of an MSI or via installutil.
当然,如果您编写了安装程序;使用自定义操作,您就可以将其用作 MSI 的一部分或通过 installutil。
回答by william
You can also force your service to run as User using ServiceProcessInstaller::Account = ServiceAccount.User;
您还可以使用ServiceProcessInstaller::Account = ServiceAccount.User强制您的服务以用户身份运行 ;
A popup asking "[domain\]user, password" will appear during service installation.
在服务安装过程中会出现一个询问“[域\]用户,密码”的弹出窗口。
public class MyServiceInstaller : Installer
{
/// Public Constructor for WindowsServiceInstaller
public MyServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.User; // and not LocalSystem;
....

