title: Starting a process remotely in Powershell, getting %ERRORLEVEL% in Windows
body: <p>A bit of background:</p>
  
  <p>I'm trying to start and stop some performance counters remotely at the start of a test, then stop them at the end of the test. I'm doing this from an automated test framework from a Win2003 machine, the test framework executes commands without launching a console, some of the system under test is running Win2008. I've written scripts to choose the performance counters based on roles assigned to the servers.</p>
  
  <p>My problem(s):</p>
  
  <ol>
  <li><code>logman</code> can't start or stop counters on machines that run a later version of the OS.</li>
  <li><code>psexec</code> can be used to run logman remotely, but psexec likes to hang intermittently when run from the test framework. It runs fine manually from the command line. I'm guessing that this is because the calling process doesn't provide a console, or some similar awkwardness. There's not much I can do about this (GRRRR)</li>
  <li>I wrote a PowerShell script that executes logman remotely using the WMI's <code>win32_process</code> and called it from a batch script, this works fine. However, the test framework decides pass and fail scenarios based on the <code>%ERRORLEVEL%</code> and the content of stderr, but WMI's <code>win32_process</code> does not give me access to either. So if the counters fail to start, the test will plough on anyway and waste everyone's time.</li>
  </ol>
  
  <p>I'm looking for a solution that will allow me to execute a program on a remote machine, check the return code of the program and/or pipe stderr back to the caller. For reasons of simplicity, it needs to be written in tools that are available on a vanilla Win2k3 box. I'd really prefer not to use a convoluted collection of scripts that dump things into log files then reading them back out again.</p>
  
  <p>Has anyone had a similar problem, and solved it, or at least have a suggestion?</p>