使用PsExec在远程pc中执行命令 - c#

时间:2015-08-19 09:55:48

标签: c# psexec sysinternals

我尝试使用将在集线器/服务器PC上运行的控制台应用程序将远程PC设置为Selenium节点。

当程序在调试模式下运行时,我在' errorMessage'

中得到以下文字
The handle is invalid.
Connecting to 200.200.20.200:5555...
Couldn't access 200.200.20.200:5555
Connecting to 200.200.20.200:5555...
  

服务器的PsExec位于:D:\ PSTools \ PsExec.exe
  服务器IP:   100.100.10.100
  远程IP:200.200.20.200
  远程PC中的jar文件保存在:D:\ Selenium \ selenium-server-standalone.jar

要在远程PC中运行的命令是

D:\Selenium>java -jar selenium-server-standalone.jar -role node -hub http://100.100.10.100/grid/register  

我在这里缺少什么

private static void StartSeleniumNode()
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.FileName = @"D:\PSTools\PsExec.exe";
            p.StartInfo.Arguments = @"\\200.200.20.200:5555 -u xyz -p abc123 -i -w D:\Selenium java -jar selenium-server-standalone.jar -role node -hub http://100.100.10.100:4444/grid/register";
            p.Start();

            string output = p.StandardOutput.ReadToEnd();
            string errormessage = p.StandardError.ReadToEnd();

            p.WaitForExit();
        }

1 个答案:

答案 0 :(得分:1)

你应该能够自己解决这个问题

您已经给了我们:p.StartInfo.Arguments = @"\\200.200.20.200"; \\what should go here

你有你想要运行的命令

java -jar selenium-server-standalone.jar -role node -hub http://100.100.10.100/grid/register

你知道要运行它的PC .. 你有psexec来获取你需要发送它的参数。

所以,它会像

一样
D:\PSTools\PsExec.exe psexec \\remotepc -i -w D:\Selenium java -jar selenium-server-standalone.jar -role node -hub http://100.100.10.100/grid/register

尝试从命令行运行它,并在命令行工作时运行。您已准备好对其进行编码(让我们假设它有效。)

您的代码将是

p.StartInfo.FileName = @"D:\PSTools\PsExec.exe";
p.StartInfo.Arguments = @"\\remotepc -i -w D:\Selenium java -jar selenium-server-standalone.jar -role node -hub http://100.100.10.100/grid/register";