Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a custom powershell host [closed]

Similar to nuget

Looking for any starter material hopefully before delving into the debugger

like image 250
Kumar Avatar asked May 16 '11 18:05

Kumar


3 Answers

There is no great documentation. I've learned most of what I know by experimentation and example. And reflection. Not the personal kind, the reverse engineering kind.

Joel Bennett's PoshConsole is probably the best OSS example of a custom host that I've found. The StudioShell codebase also contains a simple interactive host based on winforms.

Hosting can be relatively simple depending on the level of interactivity you're looking for. If you just want to run some script files, the code is as simple as this:

using( var runspace = RunspaceFactory.Create() )
{
  runspace.Open();
  using( var pipeline = runspace.CreatePipeline( "./myscript.ps1" ) )
  {
    Collection<PSObject> results = pipeline.Invoke();
    // ... process the results of running myscript.ps1
  }
}

If you need any kind of interactivity, such as prompting the user from script, you're pretty much limited drinking the koolaid and implementing most of the PSHost, PSHostUserInterface, and PSHostRawUserInterface contracts. Again, I would look at the existing living examples before diving down the rabbit hole on your own.

like image 147
beefarino Avatar answered Nov 18 '22 16:11

beefarino


MSDN has a section devoted to writing a PowerShell host in the PowerShell SDK documentation, which is a nice starting point.

Besides that a search returns the following:

  1. http://powershellstation.com/2009/10/12/writing-your-own-powershell-hosting-app-part-1-introduction/
  2. http://dougfinke.com/blog/index.php/2009/09/02/how-to-host-powershell-in-a-wpf-application/
like image 29
Bas Bossink Avatar answered Nov 18 '22 16:11

Bas Bossink


I have a series of blog posts dedicated to this.

Here's the first.

I have also implemented a debugger. I'd be glad to answer questions you might have. Contact info on my blog.

Also, here's a project that actually implemented a debugger using the API...it's the only place I've found any published code to do that.

like image 6
Mike Shepard Avatar answered Nov 18 '22 16:11

Mike Shepard