1

I'm trying to use "multi-step" command in a c# script, for example the command "net user usrname *" contains 3 steps to enter a password and then validate, i don't know if it is possible to send extra arguments while the Process is running

My code:

Process p = new Process();
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.WorkingDirectory = startupFolder;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
6
  • 1
    Redirect standard input and feed it strings. Commented Dec 16, 2017 at 16:05
  • RedirectStandardInput=true and writes string to p.StandardInput stream Commented Dec 16, 2017 at 16:16
  • i'll try this technique thx Commented Dec 16, 2017 at 16:19
  • Basic ways are creating a .bat file or using the && or || operator to tell Cmd.exe you specified more than one command. Do pay attention to error handling, the odds that this implodes in an undiagnosable mess when one of the commands fail are high. Commented Dec 16, 2017 at 16:57
  • The thing is i don't want to execute multiple commands at once, i just want to enter arguments in the command after executing, for example when you type date in the cmd the console asks you a value. Commented Dec 16, 2017 at 17:28

1 Answer 1

2

You would concatenate each command with "&". For example, "cmd /k echo Test 1 & echo test 2".

Edit:

I created a remote control/remote admin solution a while back that uses this same technique to allow you to run batch and PowerShell scripts against remote computers via the web portal. As shown in the below screenshot, it works.

enter image description here

The C# that executes the command can be found here: https://github.com/Jay-Rad/InstaTech_Client/blob/master/InstaTech_Service/Socket.cs#L614

if (cmdProcess == null || cmdProcess.HasExited)
{
    var psi2 = new ProcessStartInfo("cmd.exe", "/k " + command);
    psi2.RedirectStandardOutput = true;
    psi2.RedirectStandardInput = true;
    psi2.RedirectStandardError = true;
    psi2.UseShellExecute = false;
    psi2.WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory);

    cmdProcess = new Process();
    cmdProcess.StartInfo = psi2;
    cmdProcess.EnableRaisingEvents = true;
    cmdProcess.OutputDataReceived += async (object sender, DataReceivedEventArgs args) =>
    {
        jsonMessage.Status = "ok";
        jsonMessage.Output = args.Data;
        await SocketSend(jsonMessage);

    };
    cmdProcess.ErrorDataReceived += async (object sender, DataReceivedEventArgs args) =>
    {
        jsonMessage.Status = "ok";
        jsonMessage.Output = args.Data;
        await SocketSend(jsonMessage);
    };
    cmdProcess.Start();
    cmdProcess.BeginOutputReadLine();
    cmdProcess.BeginErrorReadLine();
}
else
{
    cmdProcess.StandardInput.WriteLine(command);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer, I tried but it don't work so i don't know what to do
Can you elaborate on "doesn't work"? Specificity is important. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.