Note that there are some explanatory texts on larger screens.

plurals
  1. POHosting PowerShell: PowerShell vs. Runspace vs. RunspacePool vs. Pipeline
    primarykey
    data
    text
    <p>I attempting to add some fairly limited PowerShell support in my application: I want the ability to periodically run a user-defined PowerShell script and show any output and (eventually) be able to handle progress notification and user-prompt requests. I <em>don't</em> need command-line-style interactive support, or (I think) remote access or the ability to run multiple simultaneous scripts, unless the user script does that itself from within the shell I host. I'll eventually want to run the script asynchronously or on a background thread, and probably seed the shell with some initial variables and <em>maybe</em> a cmdlet but that's as "fancy" as this feature is likely to get.</p> <p>I've been reading the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ee706614%28v=vs.85%29.aspx">MSDN documentation about writing host application code</a>, but while it happily explains <em>how</em> to create a <code>PowerShell</code> object, or <code>Runspace</code>, or <code>RunspacePool</code>, or <code>Pipeline</code>, there's no indication about <em>why</em> one would choose any of these approaches over another.</p> <p>I <em>think</em> I'm down to one of these two, but I've like some feedback about which approach is a better one to take:</p> <pre><code>PowerShell shell = PowerShell.Create(); shell.AddCommand(/* set initial state here? */); shell.AddStatement(); shell.AddScript(myScript); shell.Invoke(/* can set host! */); </code></pre> <p>or:</p> <pre><code>Runspace runspace = RunspaceFactory.CreateRunspace(/* can set host and initial state! */); PowerShell shell = PowerShell.Create(); shell.Runspace = runspace; shell.AddScript(myScript); shell.Invoke(/* can set host here, too! */); </code></pre> <p>(One of the required <code>PSHost</code>-derived class methods is <code>EnterNestedPrompt()</code>, and I don't know whether the user-defined script I run could cause that to get called or not. If it can, then I'll be responsible for "starting a new nested input loop" (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.host.pshost.enternestedprompt%28v=vs.85%29.aspx">as per here</a>)... if that impacts which path to take above, that would also be good to know.)</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload