【问题标题】:Copy local file remotely, execute and bring the output back [closed]远程复制本地文件,执行并带回输出[关闭]
【发布时间】:2013-01-15 15:05:15
【问题描述】:

有人可以帮我解决这个问题吗?

我在本地有一组可执行文件,需要远程运行并从它们返回输出。我有远程机器登录凭据。您能否告诉我是否有办法在 C++、C#/powershell/WMI 等中以编程方式执行此操作?

【问题讨论】:

  • 您的问题含糊不清。谁运行这些文件?来自远程计算机的某个人,或者您需要将它们上传到某个地方并从远程计算机执行?这些可执行文件会产生什么样的输出? (文件,控制台输出)
  • @Pete,我尝试使用 Invoke-Command 进行 Powershell 远程处理,但这无法将文件复制到远程机器。
  • @Steve,我的程序必须将一个 exe/bat 文件复制到远程机器,执行它并将结果带回本地机器。
  • 我认为这是一个非常明确的问题。我认为它不应该关闭。

标签: c# remoting powershell-remoting


【解决方案1】:

你应该看看结合了c# Process Class的Both PSTools。 PSTools 允许您触发进程远程机器。

一个例子:-

** 编辑 **

在远程机器上运行批处理文件的示例:-

// Create a New Process Object.
Process p = new Process();

//Assign the file you wish to execute.
p.StartInfo.FileName = "C:\\Utilities\\psexec.exe";

// We don't want a window creating for this task
p.StartInfo.CreateNoWindow = true;

// We don't want to use the operating system shell.                           
p.StartInfo.UseShellExecute = false;

// Here we set the argument to fire on the remote machine that will launch the Batc File.
p.StartInfo.Arguments = "\\\\" + RemoteMachineName + " C:\\YourBatFile.bat";

// Now to Start the Process.
p.Start();

// If you want to wait until the Process before moving on
p.WaitForExit();

这应该为您提供继续其他任务的想法。它不仅适用于打开文件。您可以使用它来安装/卸载 MsiInstaller 产品,就像使用 WMI 一样。如果要重定向输出,只需将其存储在字符串对象中即可。

【讨论】:

  • 如何使用这个复制文件到远程机器?
  • 请看我的更新。这是一件非常快速和容易的事情。我希望这会有所帮助。
  • 别想了,如果你想将文件复制到远程机器上,你只需要使用 File.Copy("FileToCopy","Destionation");您只需在每个字符串值中添加完整的目录路径。不过,您应该可以很容易地在网上解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2012-05-28
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多