1

I'm trying to write a C# console application that will launch runas.exe through cmd then run another application as that user. I've taken one of the suggestions listed below (and added a little bit) as it seems the most promising.

Process cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo("cmd.exe", @"/K C:\Windows\System32\runas.exe /noprofile /user:DOMAIN\USER'c:\windows\system32\notepad.exe\'")
{
    RedirectStandardInput = true,
    UseShellExecute = false
};
cmd.StartInfo = startinfo;
cmd.Start();
StreamWriter stdInputWriter = cmd.StandardInput;
stdInputWriter.Write("PASSWORD");
cmd.WaitForExit();

When I launch the application it asks for the password before the command itself causing there to be an error with runas.exe

enter image description here

I'm pretty sure UseShellExecute = false is causing the error but the StreamWriter doesn't work without it so I'm not sure what to do.

BlueBarren
  • 321
  • 7
  • 24
  • Did you tried: stdInputWriter.WriteLine("password"); ? Maybe you just didn't confirmed input? – Jakub Szułakiewicz Nov 28 '16 at 13:39
  • 1
    @JakubSzułakiewicz but I would still need to declare the streamwriter and if I don't do it with `Process.StandardInput` than what should I do declare it with? – BlueBarren Nov 28 '16 at 13:42

3 Answers3

2

The /c argument runs comman line and terminates, so you could not see results (and it's a large C), see her : http://ss64.com/nt/cmd.html

Try to use "/K". I did it with your command and i see results in another window.

ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/K ping PC -t");
Process.Start(startInfo);
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
1

Your process should have RedirectStandardInput = true

var p  = new Process();
var startinfo = new ProcessStartInfo("cmd.exe", @"/C C:\temp\input.bat")
{
    RedirectStandardInput = true,
    UseShellExecute = false
};
p.StartInfo = startinfo;
p.Start();
StreamWriter stdInputWriter = p.StandardInput;           
stdInputWriter.Write("y");      

This program launches input.bat, and then sends the value y to it's standard input.

For completeness, input.bat example:

set /p input=text?: 
echo %input%
Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • I'm using `"/K"` so I can see what the cmd window is doing and yet it's still closing, why is that? It was working before. Either way I'm not sure your method is working because the command I'm trying is to launch notepad and yet it isn't coming up. – BlueBarren Nov 28 '16 at 14:08
  • Running notepad with both /K and /C works in my environment, is notepad in the path? – Ofiris Nov 28 '16 at 14:58
  • The path is `c:\windows\system32\notepad.exe\` and I know it works because when I was trying @MarksimSimkin 's answer the cmd command worked properly – BlueBarren Nov 28 '16 at 15:20
0

I found a solution in the following post. I had to forgo most of the structure I was working with but I can now successfully run notepad as my desired user with the code listed below.

var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
pass.AppendChar('w');
pass.AppendChar('o');
pass.AppendChar('r');
pass.AppendChar('d');
var runFileAsUser = new ProcessStartInfo
{
    FileName = "notepad",
    UserName = "username",
    Domain = "domain",
    Password = pass,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process.Start(runFileAsUser);
Community
  • 1
  • 1
BlueBarren
  • 321
  • 7
  • 24