Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. How can i execute a "runas" cmd command through code.
The thing is all my commands work fine, but the one with runas gives error. I have to execute the command as Domain Aministrator on a remote pc. It should work fine even to open cmd prompt for inserting the password. Thanks for the help
Posted

Please find code below.

ProcessStartInfo procStartInfo = new ProcessStartInfo()
   {
       RedirectStandardError = true,
       RedirectStandardOutput = true,
       UseShellExecute = false,
       CreateNoWindow = true,
       FileName = "ABC.exe",
       Arguments = "/user:Administrator \"cmd /K " + command + "\""
   };
 
Share this answer
 
Comments
misto99 3-Dec-14 8:01am    
the fact is i dont execute an exe file, but directly the cmd command
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "runas /user:" + domain.Text + @"\" + utente.Text + "SC \\" + txtParameter.Text + "start EmpirumRC_Service";


the question is:
1. Can i hardcode the password inside the command?
or
2. Can i open a cmd prompt for inserting the password?

Thanks in advance
Try this

C#
Process p = new Process();
           ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
           startInfo.Verb = "runas";
           p.StartInfo = startInfo;
           p.Start();
 
Share this answer
 
Comments
misto99 3-Dec-14 8:13am    
not working since is not opening the prompt for inserting the password.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900