使用InstallUtil并静默设置Windows服务登录用户名/密码
-
02-07-2019 - |
题
我需要使用InstallUtil来安装C#windows服务。我需要设置服务登录凭据(用户名和密码)。所有这些都需要默默地完成。
有没有办法做这样的事情:
installutil.exe myservice.exe /customarg1=username /customarg2=password
解决方案 2
向我的同事布鲁斯·艾迪(布鲁斯·艾迪)致敬。他找到了一种方法来进行这个命令行调用:
installutil.exe /user=uname /password=pw myservice.exe
通过在安装程序类中重写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;
}
}
}
其他提示
比上面的帖子更简单,安装程序中没有额外的代码就是使用以下内容:
installUtil.exe / username = domain \ username / password = password / unattended C:\ My.exe
确保您使用的帐户有效。如果不是,您将收到“帐户名称和安全ID之间没有映射”。例外
InstallUtil.exe
设置StartupType = Manual
如果您想自动启动该服务,请使用:
sc config MyServiceName start = auto
(注意'='之后必须有空格)
不,installutil不支持。
当然,如果你写了一个安装程序;使用自定义动作然后您就可以将其用作MSI的一部分或通过installutil。
您还可以强制您的服务以用户身份运行 ServiceProcessInstaller :: Account = ServiceAccount.User ;
弹出窗口询问“[domain \] user,password"将在服务安装期间出现。
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;
....
不隶属于 StackOverflow