CSharpFeeds - All your C# feeds in one place.

Sponsors

Sunday, March 01, 2009

Provide credentials (username and password) while installing a service silently

by Christophe via Christophe on 3/1/2009 6:47:58 PM

I had to install a Windows service on several servers this week (about 30 servers …). I wanted to do it using a simple batch file, and installing the service silently.

Unfortunately, when using InstallUtil to install a Windows service, you can’t specify the credentials used by the service to logon. You can’t install your service with this kind of command line:

InstallUtil.exe MyService.exe /username=MyUserName /password=Password

I found a workaround using the OnBeforeInstall method of the Installer class. When creating a Windows service with Visual Studio, you need to create a ProjectInstaller file, and add on this component a ServiceProcessInstaller and a ServiceInstaller.

The ServiceProcessInstaller contains information about the credentials used by the service to logon. The ServiceInstaller contains general information about your Windows service: name, description, dependencies and so on.

The ProjectInstaller contains a virtual OnBeforeInstall method (or a BeforeInstall event). This method is called before installing the Windows service while using InstallUtil. In this method, you can get the parameters from the command line, and then set your proper credentials. For instance:

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
  public ProjectInstaller()
  {
    InitializeComponent();
  }

  public String GetContextParameter(String key)
  {
    if (this.Context == null
      || this.Context.Parameters == null
      || !this.Context.Parameters.ContainsKey(key))
      return null;

    return this.Context.Parameters[key].ToString();
  }

  protected override void OnBeforeInstall(IDictionary savedState)
  {
    base.OnBeforeInstall(savedState);

    String username = GetContextParameter("username");
    String password = GetContextParameter("password");

    if (!String.IsNullOrEmpty(username))
      serviceProcessInstaller1.Username = username;
    if (!String.IsNullOrEmpty(password))
      serviceProcessInstaller1.Password = password;
  }
}

Hope it helps!
email it!bookmark it!digg it!

Original Post: Provide credentials (username and password) while installing a service silently

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us