Reputation: 351
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
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.
Upvotes: 1
Views: 4465
Reputation: 351
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);
Upvotes: 0
Reputation: 6151
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%
Upvotes: 1
Reputation: 9679
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);
Upvotes: 2