5

I am trying to launch csript as an administrator (the account logged in has admin rights). setting the startinfo.verb to runas does not work.

ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cscript";
p1.Arguments = "I:\\WPKG\\wpkg.js /synchronize /quiet /nonotify";
Process p = new Process();
p.StartInfo = p1;
p.Start();

The only way I can get it to start with privileges is to manually set the username and password. However I cannot hardcode that information or put it into configurations. Is there any way to have the cmd elevate without the login info?

I have also tried adding using (System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate()) around the above code with no luck either.

Note: If I run the bat file directly, it works, if i run with password hardcoded, it works. It only fails to elevate launching from C# without login information.

JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • Obviously you need the name and password to login. Why can't you just use string concatenation to run "runas" correctly? If you are looking for an explanation of how to acquire admin rights in general look here http://stackoverflow.com/questions/3583501/c-net-acquire-administrator-rights – foowtf Nov 10 '11 at 21:30
  • I expected the ability to use the currently logged in user. The only way to have the same behavior as right click and running as admin is to code in the login info? Kind of a suck. – JeremyK Nov 10 '11 at 21:32
  • Well it's not as easy as this. If you click 'run as admin' you also have to confirm first. This is just a convenience so that you don't need to type your password every time (like you do under linux). Otherwise every tiny application could exploit you being logged in as admin. – foowtf Nov 10 '11 at 21:35
  • Well that is what the UAC is for is it not? To force the user to confirm allowing the elevation? I don't see why there is not a way to Request Elevation – JeremyK Nov 10 '11 at 21:40
  • Ok, then how exactly does your program fail to elevate the spawned process then? From what I read here: http://victorhurdugaci.com/using-uac-with-c-part-1/ Windows should display the enter-password window if you run with runas. I know, that's your problem... – foowtf Nov 10 '11 at 21:52
  • No idea, but once I figure out why setting the verb to runas does not cause the prompt, I will report back the cause. – JeremyK Nov 15 '11 at 00:47

2 Answers2

1

This utility:

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

may help. Note too the comments in that post.

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • 1
    http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/03/27/elevate-a-process-at-the-command-line-in-vista.aspx using the exe from this works. Going to inspect code to see why it works and mine does not. – JeremyK Nov 13 '11 at 22:06
  • Ah, yes, I've successfully used that one as well - +1. – Jeremy McGee Nov 14 '11 at 06:45
0

You have several choices:

  • Use the runas utility (not verb), and pass in the username and password to . You may need to do this manually once with saving the credentials (only good till a restart).
  • Pass in the username and password on the ProcessStartInfo (you will need to convert the password to a SecureString.
  • Use the runas verb (not utility) to interact with UAC, as described in this question.
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I am trying to use the runas (which I set .Verb to) however the UAC never asks to elevate. The only special case reported in that post was that UseShellExecute must be set to true, which i did. – JeremyK Nov 10 '11 at 21:46