c# - 远程复制本地文件,执行并带回输出

标签 c# remoting powershell-remoting

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

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

最佳答案

您应该考虑使用两个 PSTools 组合 c# Process 类。 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 一样。如果您想重定向输出,只需将其存储在字符串对象中即可。

关于c# - 远程复制本地文件,执行并带回输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340569/

相关文章:

powershell - 如何在 Powershell 中同时对多个服务器运行命令

azure - 将 RDP 端口更改为 443 后无法连接到 Azure VM

c# - 使用 webbrowser 控件 c# 检测网页何时完全加载

c# - C# 是否需要 .NET

c# - mvc3 多行编辑器

c# - 在 C# 中通过远程处理获取外部 IP 地址

.net - Activator.GetObject - 使用状态参数

C#:向项目添加启动画面的理想方法是什么?

session - 将变量传递给远程 session 中使用的几个脚本 block Powershell

windows - PowerShell Direct与WinRM或PowerShell Remoting有何不同