Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:26:52+00:00 2026-06-17T05:26:52+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:26:53+00:00Added an answer on June 17, 2026 at 5:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.