none
C#, Powershell and console applications - How to handle input during processing. RRS feed

  • Question

  • Part of a program that I am creating for myself allows for PowerShell commands and scripts to be run from within the program (no need to open a PowerShell cli because it is already there). For the most part, things work great, I can type in and execute commands and scripts just fine, but if it requires input before the command/script/program is done executing, it fails. What I am currently trying to get working is ftp and I am at a complete loss. If anyone knows how to get ftp (or similar programs) running through a C# runspace or any resources that explain it, I would be grateful.

     

    Edit: To simplify the post a little, I am looking for resources on how to create a fully functional Powershell CLI within my C# application. Right now, I am having issues with handling user input after a script/command/program has begun executing. For example, in a normal Powershell CLI you can start FTP and interact with it (typing help gets you a list of commands) but for my application I cannot figure out how to send text to or get text back from the ftp program.

    • Edited by w_phillips Saturday, November 13, 2010 8:30 PM Clarification
    Tuesday, November 9, 2010 1:08 AM

Answers

  • Hi w_phillips,

     

    Do you mean something like this? Interact with the powershell and redirect input/output in the C# application? Is this what you mean? Or not please feel free to point out?

     Screenshot - AsyncPowerShell_scr.png

     

    Asynchronously Execute PowerShell Scripts from C#

    Here are the basic steps to run a PowerShell script asynchronously:

    • Create a Pipeline instance by calling Runspace.CreatePipeline()
    • Pass the script to the Pipeline instance using pipeline.Commands.AddScript()
    • Feed the Pipeline its input objects using pipeline.Input.Write()
    • Close the input by calling pipeline.Input.Close()
    • Call pipeline.InvokeAsync(); this will cause the Pipeline to create a worker thread that will execute the script in the background
    • Start reading pipeline.Output for the results of the script, until pipeline.Output.EndOfPipeline becomes true

    There are two ways in which you can be notified of the availability of new output data:

    • First, there is a property pipeline.Output.WaitHandle of type System.Threading.WaitHandle. This handle can be used with the various static Wait***() methods of System.Threading.WaitHandle to wait for new data to arrive.
    • Second, pipeline.Output has an event called DataReady. By subscribing to this event, the PowerShell background thread will call you every time new data becomes available.

     

     

    If there’s anything unclear or I have misunderstood you mean, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the other community members reading the thread.

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    =================================================

    Third party disclaimer

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Sunday, November 14, 2010 10:44 AM
    Moderator

All replies

  • Hi w_phillips,

    Welcome to MSDN Forums!

     

    Can you clarify your question?

    1.       You said you create an application, and this application can run the command and script but the script cannot require an input, if there’s an input the application will be work wrong, is it right?

    And can you show us a copy of error message or the application status then we can through the information to narrow down the question.

    2.       What I am currently trying to get working is ftp and I am at a complete loss. If anyone knows how to get ftp (or similar programs) running through a C# runspace or any resources that explain it

    But I cannot understand what you mean this section. What do you mean “get ftp running through a C# runspace”?

     

    If there’s anything unclear or I had got any mistake, please feel free to let me know.

     

    Best wishes,

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Friday, November 12, 2010 8:11 AM
    Moderator
  • I may have found what I am looking on this MSDN page: Host Application Samples but have not had a chance to read through everything and try the examples out. I have made a quick edit to my original post explaining what I am trying to do, create a fully functional Powershell CLI in my C# application and allows for user input as a script/program/command executes in the event someone comes along with specifics they wish to share.
    Saturday, November 13, 2010 9:06 PM
  • Hi w_phillips,

     

    Do you mean something like this? Interact with the powershell and redirect input/output in the C# application? Is this what you mean? Or not please feel free to point out?

     Screenshot - AsyncPowerShell_scr.png

     

    Asynchronously Execute PowerShell Scripts from C#

    Here are the basic steps to run a PowerShell script asynchronously:

    • Create a Pipeline instance by calling Runspace.CreatePipeline()
    • Pass the script to the Pipeline instance using pipeline.Commands.AddScript()
    • Feed the Pipeline its input objects using pipeline.Input.Write()
    • Close the input by calling pipeline.Input.Close()
    • Call pipeline.InvokeAsync(); this will cause the Pipeline to create a worker thread that will execute the script in the background
    • Start reading pipeline.Output for the results of the script, until pipeline.Output.EndOfPipeline becomes true

    There are two ways in which you can be notified of the availability of new output data:

    • First, there is a property pipeline.Output.WaitHandle of type System.Threading.WaitHandle. This handle can be used with the various static Wait***() methods of System.Threading.WaitHandle to wait for new data to arrive.
    • Second, pipeline.Output has an event called DataReady. By subscribing to this event, the PowerShell background thread will call you every time new data becomes available.

     

     

    If there’s anything unclear or I have misunderstood you mean, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the other community members reading the thread.

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    =================================================

    Third party disclaimer

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Sunday, November 14, 2010 10:44 AM
    Moderator