繁体   English   中英

使用 PsExec 和 C# 运行远程程序 - 已连接但不工作

[英]Run remote program using PsExec and C# - connected but not working

我想从本地计算机在远程计算机上运行 .exe 文件(安装程序)。 我使用本地计算机上的 PsExec 和 Console 提示手动完成了它,并且它正在工作。 不,我需要用 C# 编写程序来自动做同样的事情。

我的代码在具有远程工作目录的远程计算机上打开控制台:“C:\\Users\\MyUser\\Desktop\\Updater”和“process.WaitForExit()”行,即使我指定 time fe “process”,它也将永远运行无休止.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;
        }
    }

不要在远程计算机上打开控制台。 通过使用ProcessStartInfo在本地计算机上执行 PsExec 打开本地计算机上的控制台。 它将在远程计算机上执行命令。

在您的问题中,您提到可以在远程计算机上打开控制台,因此您不需要使用 PsExec。 只需执行命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM