0

Im trying to execute cmd commands through c# and everything works well. How can i execute a "runas" cmd including the password, or at least how can i open the cmd prompt for inserting the password. My code execution works well, this is the only thing that i cant seem to solve. I've tried even with echo but it doesnt seem to work.

private void btnStart_Click(object sender, EventArgs e)
{

    txtResult.Text = string.Empty;
    if (txtExecutable.Text.Trim() != string.Empty)
    {
        StreamReader outputReader = null;
        StreamReader errorReader = null;
        try
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = @"/C echo " + txtpass.Text + " | runas /user:" + utente.Text + " wmic.exe /node:" + txtExecutable.Text + " ComputerSystem Get UserName && tasklist /s " + txtExecutable.Text + @" /fi ""imagename eq EmpirumRCHost.exe"" && msinfo32.exe \\" + txtParameter.Text;
            startInfo.ErrorDialog = false;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            process.StartInfo = startInfo;
            process.Start();

            bool processStarted = process.Start();
            if (processStarted)
            {
                //Get the output stream
                outputReader = process.StandardOutput;
                errorReader = process.StandardError;
                process.WaitForExit();

                //Display the result
                string displayText = "Output" + Environment.NewLine + "==============" + Environment.NewLine;
                displayText += outputReader.ReadToEnd();
                displayText += Environment.NewLine + "Error" + Environment.NewLine + "==============" +
                               Environment.NewLine;
                displayText += errorReader.ReadToEnd();
                txtResult.Text = displayText;
            }
        }
        finally
        {
            if (outputReader != null)
            {
                outputReader.Close();
            }
            if (errorReader != null)
            {
                errorReader.Close();
            }
            btnStart.Enabled = true;
        }
    }
}

I need to run the runas command as a domain user with administrative rights on a remote pc.

  • Can't you run your app as administrator? If not I think you will find the solution here: http://stackoverflow.com/questions/8690552/run-elevated-process – Jontatas Dec 03 '14 at 11:45
  • I think you need to rewrite your question, if your question is about how to pipe in some input in the standard input of the child process, try http://www.codeproject.com/Articles/18577/How-to-redirect-Standard-Input-Output-of-an-applic – BlueTrin Dec 03 '14 at 11:47
  • The thing is i need to runas as a domain administrator on a remote machine and not as local administrator. – Mihai Ghica Dec 03 '14 at 12:00

2 Answers2

0

You can try by adding application manifest file as discusses here:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Community
  • 1
  • 1
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
0

this is all you have to do run shell commands from C#

string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Ehsan
  • 4,334
  • 7
  • 39
  • 59