473,416 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Install Windows Service with Custom User Credentials

Hi 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(IDi ctionary stateSaver)
at System.Configuration.Install.Installer.Install(IDi ctionary stateSaver)
at TemplateService.TemplateServiceInstaller.Install(I Dictionary
stateSaver) in
D:\Projects\Test\Services\TemplateService\Template ServiceInstaller.cs:line 124
at System.Configuration.Install.Installer.Install(IDi ctionary stateSaver)
at System.Configuration.Install.AssemblyInstaller.Ins tall(IDictionary
savedState)
at System.Configuration.Install.Installer.Install(IDi ctionary stateSaver)
at System.Configuration.Install.TransactedInstaller.I nstall(IDictionary
savedState)

The Rollback phase of the installation is beginning.
See the contents of the log file for the
D:\Projects\Test\Services\TemplateService\bin\debu g\TemplateService.exe
assembly's progress.
The file is located at
D:\Projects\Test\Services\TemplateService\bin\debu g\TemplateService.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
Dec 4 '07 #1
1 20294
In addition, if I comment out the whole Install override, the InstallUtil.exe
utility will provide the "Set Service Login" popup, and placing the *exact*
same DOMAIN\User and password combination as is Hard Coded or through the
Parameters, the installation completes successfully. This leads me to believe
that the problem lies with InstallUtil.exe and custom parameter handling.
Again this worked in the .NET v1.1 of the Framework but not on .NET v2.0 (x86
and x64).

Since the problem with InstallUtil.exe, I decided to test further, so I
added a Setup Project (right-click Solution -Add... new Project -Other
Project Types ... Setup and Deployment -Setup Project) for my service. See
link (http://support.microsoft.com/kb/816169) for the general idea.

To work with my Installer and Install Override method, I had to add the
following to the Custom Actions -Install -Primary output ->
CustomActionData property;
"/MyUserName=[MYUSERNAME] /MyDomainName=[MYDOMAINNAME]
/MyPassword=[MYPASSWORD]".

Also I modified the Install Method slightly to handle better,
from:
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");

// Check for Custom User
if (this.Context.Parameters.ContainsKey("MyUserName") == true)

to:
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");

// Get the User name
string user = this.Context.Parameters["myUserName"];

// Check for Custom User
if (user != null && user.Length 0)

Then installing using the command line;
'setupMSIname.msi' - installs fine - uses hardcoded values.
or
'setupMSIname.msi MyUserName="{user}" MyDomainName="{domain}"
MyPassword="{password}"' - where values in between { and } are the
appropriate values, also installs fine, but with the Custom Parameterised
values.

Is there any reason why the InstallUtil.exe doesn't accept the user
credentials when passed as parameters from a batch file/command line, except
when run from a MSI Setup (if it does internally use the InstallUtil.exe)??

Hopefully I am missing something completely in which I can then humbly
accept and fix so that my InstallUtil.exe implementation works as required.

TIA
Dec 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marina | last post by:
Hi, The windows service has reference to some custom DLL's, which are located in "subfolder/anotherfolder" relative to the service exe. There is a configuration file for the .exe which...
1
by: csheng | last post by:
I have a application to deliver now, and I am stuck. I developed a windows service, and I use intallutil to install. It works fine in development machine, when I copy the file to a windows 2003...
0
by: MarkM | last post by:
Hi there. Since upgrading to XP SP2 on my development box, I now compile the same windows service code and try to install it on a Windows 2000 server. Throws error "Access Is Denied". The...
2
by: Ant | last post by:
Does anyone have an example of how to install a windows service from .net code. I've seen PInvoke versions, but it should be able to be done using System.Configuration.Install.Installer as the...
2
by: Shani | last post by:
Could someone suggest me as to how to install an Windows Service in C#? I do not want to use InstallUtil as I need to pass a parameter, which is not possible. I do not want to use write an...
3
by: Jeremy S. | last post by:
On my dev machine (XP/Pro with VS.NET 2003) I have been developing a Windows Service and installing it on the local machine by opening the Visual Studio Command Prompt and then executing . Now I...
8
by: Stanley | last post by:
I have finished a Windows Service, now I'm trying to deploy the service to all my local computers. I want to ask if there is a fast way, rather then install it one by one? All answer would be...
3
by: =?Utf-8?B?Um9iZXJ0IFNsYW5leQ==?= | last post by:
I'm trying to install a .NET Windows Service using the ServiceInstaller / ServiceProcessInstaller classes. I cannot seem to find a way to specify command line arguments. I've looked through...
0
by: crowl | last post by:
Following scenario: client (c# app) -asp.net web service - sql server The client hast to authenticated via Basic authentications. This user account should be used to access the sql server on...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.