How to write a custom powershell host [closed]

被刻印的时光 ゝ 提交于 2019-11-27 12:32:49

问题


Similar to nuget

Looking for any starter material hopefully before delving into the debugger


回答1:


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/



回答2:


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.




回答3:


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.




回答4:


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)



来源:https://stackoverflow.com/questions/6021662/how-to-write-a-custom-powershell-host

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