Question

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.

Was it helpful?

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.

OTHER TIPS

From the docs on the Process object it will only allow you to start & stop local processes. Therefore if you want to start & stop remote processes then you need to use either PowerShell remoting or a tool like psexec. I see your C# comments indicate you would set up a remote runspace. That works great if you have PowerShell v2 on each remote computer, have remoting enabled, preferably are on a domain and have the appropriate admin credentials. IIRC the C# process setting up the remote runspace must run as admin and the credentials it uses must have admin rights on the remote computer (unless you've set up special remoting endpoints on each of the remote computers).

If you do have PowerShell v2 or v3 on each remote computer and can enable remoting (just run Enable-PSRemoting -force) then you should easily be able to do what you want. However, there is no need to drop down to use the .NET Process object. Just use PowerShell cmdlets. Put something like this in a script, add that to the pipeline your create and invoke that.

$p = Start-Process Echo.exe -Arg "output from echo.exe" -PassThru
$p | Wait-Process -Timeout 3 # 3 seconds
if (!$?) { # Success code $? returns $false if Wait-Process times out
    $p | Stop-Process
}

For async support, try it this way:

$script = {
    $pid
    ipconfig.exe /all
    $LastExitCode
}
$job = Invoke-Command localhost $script -AsJob
do {
    Receive-Job $job
}
while (!(Wait-Job $job -Timeout 1))
Receive-Job $job    
Remove-Job $job

With the returned $pid, you can use that along with WMI to find all the child process to kill them if necessary like so:

$childPids = @(Get-WmiObject -Class Win32_Process -Filter "ParentProcessID=$PID" |Select-Object -Property ProcessID)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top