3

I have been having trouble passing input programmatically to the system net user program using C#. I am trying to activate and set a password for a certain user account. By my investigations, it seems like the process finishes before anything can be passed. I do not know why the background net user program does not wait for input before exiting.

Here is the command I am running programmatically to accomplish this:

net user username /active:yes & net user username *

The output of the second command is as follows:

Type a password for the user:
Retype the password to confirm:
The command completed successfully

If you were to run the above command manually, it would ask you for the password and hide what you're typing from the screen. However, the program doesn't seem to stop when ran programmatically.

To call the program, I have a function that starts the program and returns the process to another function, which sends input to the process. Here is the first function:

static Process RunCommandGetProcess(string command)
    {
        Process process = new Process();
        ProcessStartInfo psInfo = new ProcessStartInfo();

        psInfo.FileName = "CMD.exe";
        psInfo.Arguments = "/C " + command + "& PAUSE";

        // Allow for Input redirection
        psInfo.UseShellExecute = false;
        psInfo.RedirectStandardInput = true;

        // Window style
        psInfo.WindowStyle = ProcessWindowStyle.Normal;

        // Start the mothertrucker!
        process.StartInfo = psInfo;
        process.Start();

        return process;
    }

And the Calling function:

static int ActivateUserWithPassword(string password)
{
    // Start net user with that other function
    Process process = RunCommandGetProcess("net user username /active:yes & net user username *");

    StreamWriter streamWriter = process.StandardInput;

    streamWriter.WriteLine(password);    // First Prompt
    streamWriter.WriteLine(password);    // Second Prompt

    process.WaitForExit();

    return process.ExitCode;
}

However, When I run the debugger, the commands complete successfully before the two streamWriter.WriteLine(password); lines are even met! I have tried Googling, but came to no avail.

You people are my only hope.

3
  • I think that your command does not wait for the password and immediately ends after the start. You can check this by running the command manually? Commented Jul 15, 2017 at 6:22
  • @Dmitry is that a typo at the end? When I ran the debugger and placed breakpoints at the two WriteLine's, the command finished executing with no input before it even reached it. Although. I may be sending input to the first command unintentionally and the breakpoints just misaligned them. Commented Jul 15, 2017 at 6:33
  • @MasonSchmidgall Check out my solution and let me know if it worked. Commented Jul 15, 2017 at 11:01

1 Answer 1

2

Okay! I have been highly motivated to solve your issue as soon as i saw it!. after 2 hours of non-stop debugging, i have a solution for you!.

Issue No .1 : Your application does not have Administrator privileges, that is why the command exits right after you start. Start the .exe with Admin Privileges.

Issue No. 2 : Since the password is masked in the input, Even if i did a streamWriter.WriteLine(password) once,The output is "Command Executed Successfully". There is no way of knowing if the password was actually passed or it took empty string as the password.

Solution

You can use the net user command with a parameter for password like this net user user_name password. No need to prompt the user for password i.e Don't use '*' after username ,since you are passing it through the program.

So this is how it works

Process proc = new Process();
        ProcessStartInfo start = new ProcessStartInfo();       
        start.FileName = "cmd";
        start.Arguments = "/k";
        start.RedirectStandardInput = true;
        start.WorkingDirectory = Environment.CurrentDirectory;
        start.UseShellExecute = false;
        proc.StartInfo = start;
        proc.Start();
        proc.StandardInput.WriteLine("net user \"username\" password");

Very Very Important!!

Start the exe as an administrator OR you will have to do this

start.UseShellExecute = true;
start.Verb = "runas";

But then you wont be able to redirect the Output/Input streams!.

Sign up to request clarification or add additional context in comments.

1 Comment

I should have mentioned that the command was running all the way through properly and that I was running my program as Administrator. However, I did not know that you could pass a password. I will try your solution as soon as I can. I appreciate the time and effort you put in for me!

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.