-2

I have a problem with remotely starting a proccess with psexec.exe. My solution looks like this, I have on server a cmd script, that runs my console application. The applications job is to start a test run with vstest.console.exe with the help of following code:

  var config = LoadConfigFromFile<DeploymentStorage>(configFile);

  var currentDateTime = DateTime.Now;
  var resultFileName = String.Format("testresults{0}{1}{2}{3}{4}{5}.trx",
      currentDateTime.Year, currentDateTime.Month, currentDateTime.Day, currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second);

  ProcessStartInfo processInfo =
      new ProcessStartInfo(config.TestSettings.First(x => x.Key == "MSTestLocation").Value,
      "\"" + config.TestSettings.First(x => x.Key == "TestContainer").Value + "\" /logger:trx;LogFileName=\"" + 
      config.TestSettings.First(x => x.Key == "TestResultLocation").Value + resultFileName + "\"");
  processInfo.CreateNoWindow = true;
  processInfo.UseShellExecute = false;
  processInfo.RedirectStandardError = true;
  processInfo.RedirectStandardOutput = true;

  var process = Process.Start(processInfo);

  process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
      Console.WriteLine("output>>" + e.Data);
  process.BeginOutputReadLine();

  process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
      Console.WriteLine("error>>" + e.Data);
  process.BeginErrorReadLine();

  process.WaitForExit();

  var exitCode = process.ExitCode;

  Console.WriteLine("ExitCode: {0}", exitCode);
  process.Close();

If I run this script localy on the server everything works fine, the test run starts. But if I start this script remotely through my local computer with the help of psexec.exe I get the following error:

Failed to queue test run '[Servername] 2018-04-24 15:17:20': Unable to start the agent process.

It's not the problem of psexec.exe, with this tool I can run other programs and cmd scripts, I can directly run the vstest.console.exe with it too, but I need to run the tests through my console app.

Why psexec.exe is not running my app? Do I miss something in the configuration of the process?

EDIT: I run psexec.exe with the following command on my local computer:

C:\Programs\PSTools\psexec.exe \\my-server -h C:\TestApp\MyTestTool.exe TEST C:\TestApp\base.config.xml

Where TEST and path to base.config.xml are arguments passed to the console app.

EDIT2: Well, it seems, that I have misinterpreted my problem. I run psexec.exe command from command line on my local computer. It should run my console app called TestApp at the server. This console app on the server should start vstest.console.exe with the help of the code above, not the psexec.exe and it can't do it. Hope that this makes it a litle clearer.

EDIT3: Ok, I know now what is cousing the problem, I can not run tests remotely, if my test container is a *.orderedtest file. If I try to run test setting a dll as test container, everything works fine.

Roman Suska
  • 527
  • 2
  • 7
  • 21
  • 1
    Where's the part where you start `psexec` in your code? Please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – 41686d6564 stands w. Palestine Apr 26 '18 at 07:29
  • Figure out which command you're trying to execute, try executing it manually, does it work then? – Lasse V. Karlsen Apr 26 '18 at 07:56
  • Yes, if I run my TestApp locally on the server it works. As far I figured it out that the psexec fires my TestApp remotely, but then the TestApp has problems with starting the vstest.console.exe with the code above. – Roman Suska Apr 26 '18 at 08:10

1 Answers1

0

In general there are a few things that you need to do when trying this. I have executed psexec from C# before, unfortunately I no longer have the code for doing this remotely only locally, but some of these will apply.

1) You may need to execute your application with administrator rights

See the answers here for how to do this: Giving application elevated UAC

2) Make sure you have the correct arguments enabled for PSEXEC. You need to make sure that the user you are trying to execute under on the remote machine has the correct access rights.

I suggest to try executing the PSEXEC exactly as you require from an elevated local CMD prompt to make sure this is correct.

Full arguments can be found here: https://learn.microsoft.com/en-us/sysinternals/downloads/psexec

Then in your code make sure that PSEXEC is specified as the FileName and everything else is specified as arguments.
Make sure that paths are enclosed with double quotes

Here is an example I have used;

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.FileName = "C:\Data\Utils\psexec.exe";
startInfo.Arguments = "-u xxxx -p xxxx -accepteula" + " " + "\\\\remotemachine" + " " + "\"c:\\full\\path\\on\\remote\\machine\\myexecuteable.exe\"" + " " + " - other - agrs";
Process nProc = Process.Start(startInfo);
nProc.WaitForExit();
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • Thanks for reply. The problem is I'm not trying to run the psexec from console app. I try to run with the help of psexec an app that is on the server, which job is to fire another app, in my case the vstest.console.exe. – Roman Suska Apr 26 '18 at 08:06