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;
}
}