2

I have a batch file I wrote (proof of concept) to install a sample service I also created. It contains the following command line:

sc create serviceTest binPath= C:\Sandbox\ServiceTest\ServiceTest.exe DisplayName= "Service Test"

When I right click and select 'Run as Administrator' everything runs as expected. Running the batch file without doing this gives 'Access Denied'. This is proof that the command works.

I have also tried running the sc command via Process.Start():

const string sc = @"sc";
const string arguments = @"create serviceTest binpath= {0} DisplayName= ""Service Test""";
FileInfo fi = new FileInfo(this.txtServiceLocation.Text);
if (fi.Exists)
{
    ProcessStartInfo psi = new ProcessStartInfo()
    {
        FileName = sc,
        Arguments = string.Format(arguments, fi.FullName),
        UserName = "admin",
        Password = "secret".ToSecureString(),
        Domain = "MYDOMAIN",
        WorkingDirectory = fi.DirectoryName,
        CreateNoWindow = true,
        UseShellExecute = false
    };
    Process.Start(psi);             
}
else
    MessageBox.Show("The service exe could not be found."); 

I need to be able to do this programatically. What do I need to do to get this to work?

CLARIFICATION: I need to be able to do this without a user being prompted. This code will be running under a custom tfs build process.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
  • 1
    As you mention in your question, it does work when invoked from a Administrator user (_Run as Administrator_), so you already know how to get it to work. Limited users can't install Windows Services, so you're basically out of luck. – M.Babcock Apr 17 '12 at 20:20
  • 1
    possible duplicate of [How to force C# App to run as administrator on Windows 7](http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7) – Hans Passant Apr 17 '12 at 20:53

3 Answers3

1

In Windows 7 by default, runs most applications with least privilege access (non-admin) control. As your application is trying to modify the system, it needs to be elevated to Admin privilege in order to run successfully. If you want to make this app to run in admin privilege permanently, please follow the instructions as in the link

http://www.groovypost.com/howto/microsoft/automatically-run-any-application-as-admin-in-windows-7/

Cinchoo
  • 6,088
  • 2
  • 19
  • 34
0

You need to add a UAC manifest file to your assembly in order to force UAC to run the process as an administrator. For a reference to other solutions, you can see UAC need for console application.

Community
  • 1
  • 1
Espen Burud
  • 1,871
  • 10
  • 9
  • Unfortunately this is going to be run from tfs as part of a custom process and so no one will be able to click OK to run as administrator. I need this to work completely without user intervention. – Mike Cheel Apr 18 '12 at 12:47
0

While I never got this to work I can still use impersonation via code and also ensure that the tfs build account has appropriate access. This is what I had to do.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101