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.
您可以通过添加应用程序清单文件尝试为讨论在这里 :
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.