How to write a custom powershell host [closed]

By : Kumar
Source: Stackoverflow.com
Question!

Similar to nuget

Looking for any starter material hopefully before delving into the debugger

By : Kumar


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.

By : beefarino


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.



This video can help you solving your question :)
By: admin