Install Windows Service with Custom User Credentials
am 04.12.2007 18:01:01 von B.AhlstedtHi all,
This is something that I have been toying with for about a week now. What
I want to achieve is Install a Service with Customised parameters (using
InstallUtil.exe) for User Name. Example (C#);
[RunInstaller(true)]
public class MyServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller serviceInstaller;
public MyServiceInstaller()
{
this.serviceProcessInstaller = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
// Null out UserName and password
//POINT1
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;
// this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;
// Service Installer, set ServiceName to same as ServiceName in
ServiceBase derived class
this.serviceInstaller.ServiceName = "MyService";
// Add Both Installers to Project Installers Collection to be Run
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller,
this.serviceInstaller});
}
//POINT2 Override Install Method to add customised values
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");
// Check for Custom User
if (this.Context.Parameters.ContainsKey("MyUserName") == true)
{
Console.WriteLine("******* Using CUSTOMISED values ....");
this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;
this.serviceProcessInstaller.Password =
this.Context.Parameters["MyPassword"];
string user = this.Context.Parameters["MyUserName"];
string domain = this.Context.Parameters["MyDomainName"];
// Do we use Principal Name @ Authority or
Authority\Principal ( Domain\User )
string userName = string.Empty;
if (domain.IndexOf(".") > -1 && domain.Length > 1)
{
userName = string.Concat(user, "@", domain);
}
else
{
userName = string.Concat(domain, "\\", user);
}
// Update the Correct user Name
this.serviceProcessInstaller.Username = userName;
}
else
{
Console.WriteLine("******* Using HARDCODED values ....");
this.serviceProcessInstaller.Password = "password";
this.serviceProcessInstaller.Username = "DOMAIN\\User";
this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;
}
Console.WriteLine("******* Username: {0}, Account: {1},
Password: {2}",
this.serviceProcessInstaller.Username,
this.serviceProcessInstaller.Account,
this.serviceProcessInstaller.Password);
// Call the Base Class to finish Installation
base.Install(stateSaver);
Console.WriteLine("Install End...");
}
}
POINT1 - I have nulled the User Name and Password Fields and left the
Account Type as default in the MyServiceInstaller Constructor. I could
hard-code the values here, but that defeats the purpose of the exercise.
POINT2 -
a) Running the command "InstallUtil MyService.exe" generates the following
Console Log (minus .NET. Framework Installation ...);
Install Start...
******* Using HARDCODED values ....
******* Username: DOMAIN\User, Account: User, Password: password
Install End...
b) Running the command "InstallUtil /MyUserName=User /MyDomainName=DOMAIN
/MyPassword=password MyService.exe" generates; (after Uninstall if above was
run first "InstallUtil /u MyService.exe")
Install Start...
******* Using CUSTOMISED values ....
******* Username: DOMAIN\User, Account: User, Password: password
Then Error Details;
An exception occurred during the Install phase.
System.ComponentModel.Win32Exception: The account name is invalid or does
not exist, or the password is invalid for the account name specified
at System.ServiceProcess.ServiceInstaller.Install(IDictionary stateSaver)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at TemplateService.TemplateServiceInstaller.Install(IDictionary
stateSaver) in
D:\Projects\Test\Services\TemplateService\TemplateServiceIns taller.cs:line 124
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.AssemblyInstaller.Install(IDict ionary
savedState)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.TransactedInstaller.Install(IDi ctionary
savedState)
The Rollback phase of the installation is beginning.
See the contents of the log file for the
D:\Projects\Test\Services\TemplateService\bin\debug\Template Service.exe
assembly's progress.
The file is located at
D:\Projects\Test\Services\TemplateService\bin\debug\Template Service.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
The machines that this occurs on are Windows XP Pro and XP Pro 64 (with
their respective x86 and x64 InstallUtil versions) and using .NET Framework
2.0, both connected to a Domain Controller. Changing the User Credentials to
a Local User, Domain Administrator still offers no difference, Hard coded
within Installer works, but using passed custom values to the Install Utility
seems to create some corruption on the install process.
This worked with InstallUtil in .NET v1.1 so am at a loss as to why
Hardcoding works but using the Context.Parameters StringDictionary creates
some kind of Error that InstallUtil cannot handle.
Anyone able to shed some light on this?
TIA Bjorn Ahlstedt