How to write a custom powershell host [closed]

▼魔方 西西 提交于 2019-11-28 19:52:50

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.

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.

You'll find here under the blog from which I built my own PowerShell which interpret scripts without showing any windows.

Blog from Leporelo

It's really the few first lines you need and the shortcut to PowerShell SDK.

the shell responding my own need (sorry in french)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!