问题描述
我是听有关PowerShell 2.0中的播客。在那个情节,他们谈到的可能性扩展应用程序,以公开为PowerShell的功能。
I was listening to the podcast about PowerShell 2.0 that Scott Hanselman did. In that episode, they talked about the possibilities to extend your application to expose functionality for PowerShell.
现在这个听起来很有趣。
有任何你做,在你的应用程序公开的功能,例如扩展?
。如果是这样,什么样的functionallity的
Now this sounds interesting.Have any of you done such extensions that exposes functionality in your application?If so, what kind of functionallity?
编辑:
更准确地说:我没有兴趣在我的应用程序托管的PowerShell。我更感兴趣的是与API的界面打开了我的申请。因此,一个PowerShell提供好像是我在寻找更深入的技术。
To be more precise: I am not interested in hosting PowerShell in my application. I'm more interested in opening up my application with an API-like interface. So a PowerShell Provider seems to be the technology I'm looking for more insight in.
推荐答案
有多种方法来利用PowerShell的在自定义应用程序。 PowerShell的自动化引擎的优点在于你不必应付处理参数,格式化输出,以及一堆,你将不得不自己处理其他事情PITA。抛出一个物体倒在PowerShell中的管道,你重写ProcessRecord方法,并调用的writeObject
There are multiple ways to leverage PowerShell in a custom application. The beauty of the PowerShell Automation engine is that you don't have to deal with handling parameters, formatting output, and bunch of other PITA things that you would have to handle yourself. To throw an object down the pipeline in PowerShell, you override the ProcessRecord Method and call WriteObject
protected override void ProcessRecord()
{
// Get the current processes
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline making them available
// to the next cmdlet. The second parameter of this call tells
// PowerShell to enumerate the array, and send one process at a
// time to the pipeline.
WriteObject(processes, true);
}
您可以编写的cmdlet允许管理员为应用程序的服务器端自动化。的cmdlet总是与动词 - 名词命名约定的功能基于任务的单位。例如,获取进程,或重新启动-服务。 cmdlet的做一件事,做得很不错。有各种附带相结合的cmdlet一起不可思议的力量的。
You can write Cmdlets that allow an admin to automate the server side of an application. Cmdlets are always task based units of functionality with verb-noun naming convention. For example, Get-Process, or Restart-Service. Cmdlets do one thing and do it very well. There is all kinds of incredible power that comes with combining cmdlets together.
另外,如果您的应用程序有一些类型的数据存储,它也可以写一个供应商这将允许某人浏览和/或管理使用CMDS如CD(设置位置)和MD(新项目)的数据存储。提供程序是什么PS团队写了那么你可以光盘与CD HKLM注册表:或CD证书的证书存储:
Also, if your app has some sort of data store, its also possible to write a provider which would allow someone to browse and/or manage the data store using cmds like cd (set-location) and md (new-item). A provider is what the PS team wrote so you can cd into the registry with cd hklm: or the certificate store with cd cert:
您也可以举办PowerShell的本身的应用程序。
You can also host PowerShell itself in your application.
有是MSDN上这三个选项的
There is some good information on all three of these options on MSDN here
考虑到供应商的利益,这里有关于如何创建PowerShell提供。
Considering the interest in providers, here are some examples on how to create a PowerShell Provider.
有一直很少关于设计和使用一组小命令的或以暴露与提供商东西讨论。当你实现一个供应商,你也可以得到一些的cmdlet,如Get-项目,新项目,获取地点,设置地点。然而,我发现,具有除了提供特定任务的cmdlet一些可以是非常有帮助的。
There has been a few discussions about design and using a set of cmdlets or to expose something with a Provider. When you implement a provider, you also get a few cmdlets, such as Get-item, New-item, Get-Location, Set-Location. However, I have found that having some cmdlets for specific tasks in addition to a provider can be very helpful.
这篇关于如何从应用程序中暴露功能的PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!