1
votes

I want to run .exe file (installer) at remote computer from local computer. I did it manually using PsExec and Console prompt from my local computer and it is working. No I need to write program in C# to do same thing automaticly.

My code is opening console at remote computer with remote working directory: "C:\Users\MyUser\Desktop\Updater" and at line "process.WaitForExit()" it is running forever with no end even if I specify time f.e. "process.WaitForExit(10000)".

Program is not starting my exe.file. I checked it in remote task manager. Adding ""C:\Users\MyUser\Desktop\MyInstaller\Installers"" -s -update";" as arguments are essential because my installer needs console prompt. How to fix it?

static string hostname = @"111.111.1.11";
        static string username = "myusername";
        static string password = "mypassword";
        string commandToRunLocal = $@"psexec \\{hostname} -i 1 -w ""C:\Users\MyUser\Desktop\Updater"" -u {username} - p {password} cmd";
        string commandToRunRemote = @"DLLConfiguration.exe ""C:\Users\MyUser\Desktop\MyInstaller\Installers"" -s -update";

    public string RunRemoteInstaller()
    {
        try
        {
            Process process = new System.Diagnostics.Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.FileName = "cmd.exe";
            startInfo.CreateNoWindow = false;
            process.StartInfo = startInfo;
            startInfo.WorkingDirectory = @"C:\Users\myUserName";
            process.Start();
            process.StandardInput.WriteLine(commandToRunLocal);
            process.StandardInput.Flush();
            process.StandardInput.WriteLine(commandToRunRemote);
            process.StandardInput.Flush();
            process.StandardInput.Close();
            process.WaitForExit();
            string error = process.StandardError.ReadToEnd();
            string consoleOutput = process.StandardOutput.ReadToEnd();

            if (process.ExitCode == 0 && null != process && process.HasExited)
            {
                return process.StandardOutput.ReadToEnd();
            }
            else
            {
                return "Error running the command";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
1
In the PsExec, the command is C:\Users\MyUser\Desktop\Updater But I don't see an extension. What type of file is it? Are you missing the .bat or .exeBlack Frog
This path is just a working directory. I perform executing .exe file in the next step.YoungEddie
PsExec is used to perform the command, not open a folder.Black Frog
@YoungEddie, did you ever solve this? I'm running into the same thing --- works if I run PsExec manually, but just seems to hang (until timeout) when I run it from within C#. In my case, I'm trying to run PsExec through a C# web page. Thanks :)Ted Krapf

1 Answers

1
votes

Don't open a console on the remote computer. Open a console on the local computer by using ProcessStartInfo to execute the PsExec on your local machine. It will execute the command on the remote computer.

In your question, you mention you can open a console on the remote computer, therefore you don't need to use PsExec. Just execute the command.