【发布时间】:2019-04-29 15:13:30
【问题描述】:
我想从本地计算机在远程计算机上运行 .exe 文件(安装程序)。我使用本地计算机上的 PsExec 和控制台提示手动完成了它,它正在工作。不,我需要用 C# 编写程序来自动执行相同的操作。
我的代码正在使用远程工作目录打开远程计算机上的控制台:“C:\Users\MyUser\Desktop\Updater”并在“process.WaitForExit()”行,即使我指定了时间,它也将永远运行,没有尽头铁“进程.WaitForExit(10000)”。
程序没有启动我的 exe.file。我在远程任务管理器中检查了它。添加 ""C:\Users\MyUser\Desktop\MyInstaller\Installers"" -s -update";"因为参数是必不可少的,因为我的安装程序需要控制台提示符。 如何解决?
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;
}
}
【问题讨论】:
-
在 PsExec 中,命令是 C:\Users\MyUser\Desktop\Updater 但我没有看到扩展名。它是什么类型的文件?您是否缺少 .bat 或 .exe
-
这个路径只是一个工作目录。我在下一步中执行 .exe 文件。
-
PsExec 用于执行命令,而不是打开文件夹。
-
@YoungEddie,你解决过这个问题吗?我遇到了同样的事情——如果我手动运行 PsExec 就可以工作,但是当我从 C# 中运行它时似乎会挂起(直到超时)。就我而言,我正在尝试通过 C# 网页运行 PsExec。谢谢:)
标签: c#