Search code examples
c#powershellprocesscancellation

Cancellable remote processes and capturing output


I'm trying to use PowerShell via its C# API to do the following:

  1. start remote processes
  2. terminate them
  3. capture standard and error output of remote processes (not after the process terminates, but as that output is being produced).
  4. capture the exit code of remote processes.

I'm starting remote processes with the following PowerShell script:

$ps = new-object System.Diagnostics.Process
$ps
$ps.StartInfo.Filename = "c:\Echo.exe"
$ps.StartInfo.Arguments = ""
$ps.StartInfo.RedirectStandardOutput = $true
$ps.StartInfo.RedirectStandardError = $true
$ps.StartInfo.UseShellExecute = $false
$ps.start()
$ps.WaitForExit()
$ps.ExitCode

The C# part of my prototype looks like the following:

// Note: in this example the process is started locally
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*_remotePcConnectionInfo*/))
{
    runspace.Open();

    Pipeline pipeline = runspace.CreatePipeline();
    // Scripts.RunExecutable is that script above
    pipeline.Commands.AddScript(Scripts.RunExecutable);

    pipeline.InvokeAsync();

    var process = (Process)pipeline.Output.Read().BaseObject;

    bool started = (bool)pipeline.Output.Read().BaseObject;

    // Not showing the dummy event handlers - they simply do a Console.WriteLine now.
    process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
    process.BeginOutputReadLine();

    // Not showing the dummy event handlers - they simply do a Console.WriteLine now.
    process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
    process.BeginErrorReadLine();

    int processExitCode = (int) pipeline.Output.Read().BaseObject;
}

This does capture the output of processes run locally, but will this work for remote processes as well? (Another, less important question is: how can this work for remote processes? Is .Net Remoting involved in some way and am I getting a proxy for Process?) If not, what's the way to do that? Do mind that I need the output as it's being produced, not after the process is terminated.

This does not capture process termination. I've tried doing termination by capturing process Id first and then running "Stop Process " PowerShell script from a different runspace. That failed, because "the pipeline is already running" and pipelines cannot run in parallel... Then I've tried invoking process.Kill() from C#, that worked for my local process, but SO reports it won't work for remote processes... Then I've tried tuning my PowerShell script so it included a global variable and a waiting loop, but I never figured out how to set that variable after the pipeline is started. The added loop looked like this:

while ($ps.HasExited -eq $false -and $global:cancelled -eq $false)
{
    Start-Sleep -seconds 1
}

if ($global:cancelled -eq $true)
{
    $ps.Kill()
}

So, that failed, too. Any advice on process termination for my scenario?

Is PowerShell even a good fit for this? (we're strongly against openSSH we've tried using before because of its issues with forking).

UPDATE: What I've ended up doing was calling (via C# starting a process -- "powershell" + "-Command ...") into powershell.exe that was told to execute a script (invoke-command) on a remote computer. Powershell captures output produced on remote PC by default, so I could easily get this of the Process instance. When I wanted to cancel, I killed the local process. The return code of a remote command was trickier. I will post the command, script and C#-snippet in a while.


Solution

  • Powershell Jobs can be executed remotely and will capture their output and return it to the originating machine. With this you wouldn't have to create a remote runspace in C#, just a local one.

    The powershell script to do this would look like this:

    $job = invoke-command -computername remotecomputer -scriptblock { start-process "C:\Echo.exe" -wait } -asjob
    while ( $job.State -eq "Running" ) {
      wait-job -job $job -timeout 1
      receive-job -job $job -keep # print the current output but keep all output for later.
    }
    
    receive-job -job $job
    

    You would probably just need to tweak the scriptblock parameter to invoke-command to get the exit code if wanted that in the output as well.