如何在应用程序中向PowerShell公开功能

时间:2021-08-12 00:34:49

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.

我正在收听Scott Hanselman所做的关于PowerShell 2.0的播客。在那一集中,他们讨论了扩展应用程序以展示PowerShell功能的可能性。

Now this sounds interesting. Have any of you done such extensions that exposes functionality in your application? If so, what kind of functionallity?

现在听起来很有趣。你有没有做过这样的扩展,在你的应用程序中公开功能?如果是这样,什么样的功能?

EDIT: 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不感兴趣。我更感兴趣的是使用类似API的界面打开我的应用程序。因此,PowerShell提供商似乎是我正在寻求更多洞察力的技术。

3 个解决方案

#1


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

在自定义应用程序中有多种方法可以利用PowerShell。 PowerShell自动化引擎的优点在于您无需处理处理参数,格式化输出以及您必须自己处理的其他PITA内容。要在PowerShell中将对象抛出管道,可以重写ProcessRecord方法并调用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);
}

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.

您可以编写允许管理员自动化应用程序服务器端的Cmdlet。 Cmdlet总是基于任务的功能单元,具有动词 - 名词命名约定。例如,Get-Process或Restart-Service。 Cmdlet做一件事并做得很好。将cmdlet组合在一起会带来各种令人难以置信的强大功能。

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:

此外,如果您的应用程序具有某种数据存储,则还可以编写一个提供程序,允许某人使用cmd(如cd(set-location)和md(new-item))浏览和/或管理数据存储。提供者是PS团队编写的,因此您可以使用cd hklm:或使用cd cert的证书存储区进入注册表:

You can also host PowerShell itself in your application.

您还可以在应用程序中托管PowerShell。

There is some good information on all three of these options on MSDN here

这里有关于MSDN上所有这三个选项的一些很好的信息

Considering the interest in providers, here are some examples on how to create a PowerShell Provider.

考虑到对提供程序的兴趣,以下是有关如何创建PowerShell提供程序的一些示例。

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.

有一些关于设计和使用一组cmdlet或者使用Provider公开某些内容的讨论。实现提供程序时,还会获得一些cmdlet,例如Get-item,New-item,Get-Location,Set-Location。但是,我发现除了提供程序之外,为特定任务设置一些cmdlet可能非常有用。

#2


Here's a good book on this side of powershell (which doesn't get much coverage in most books).

这是一本关于powershell这方面的好书(在大多数书中都没有得到很多报道)。

Professional Poweshell Programming

专业的Poweshell编程

Using this book (and some trips to msdn), I was able to have a custom powershell host up and runnning in a day. A few days later, I had custom cmdlets that interact with the gui (to build treeviews, grids, diagrams, menus, etc.)

使用这本书(以及对msdn的一些旅行),我能够在一天内完成并运行自定义PowerShell主机。几天后,我有了与gui交互的自定义​​cmdlet(用于构建树视图,网格,图表,菜单等)

#3


There are a couple of levels of PowerShell support, and not many people explain this well.

有几个级别的PowerShell支持,并没有多少人解释这一点。

  1. .NET classes. Yes, anything with a .NET API can be accessed by PowerShell without you doing anything extra! But, you don't get quite all of the extra PowerShell features. The least amount of work.
  2. .NET类。是的,PowerShell可以访问任何带有.NET API的内容,而无需您做任何额外的事情!但是,您没有获得所有额外的PowerShell功能。最少的工作量。

  3. A PowerShell module with cmdlets, scripts, and/or functions. This will require writing some PowerShell specific code to wrap around your existing API. If you go this route, you should sit down and learn the best practices for PowerShell design and think out your PowerShell support. This is more work required to do it right.
  4. 具有cmdlet,脚本和/或函数的PowerShell模块。这将需要编写一些PowerShell特定代码来包装您现有的API。如果你走这条路,你应该坐下来学习PowerShell设计的最佳实践,并考虑你的PowerShell支持。这是做正确的工作所需的更多工作。

  5. PowerShell providers. Providers are a special case, not everything should have a provider. You need to really consider whether your information really makes sense in a file system view.
  6. PowerShell提供商。提供商是一个特例,并非一切都应该有提供者。您需要在文件系统视图中真正考虑您的信息是否真的有意义。

This doesn't apply to your question, but I'll add it for completeness.

这不适用于您的问题,但我会将其添加为完整性。

  1. Hosted PowerShell. You can host PowerShell code in your app, or access PowerShell functionality from your app. This makes sense in some situations, but is also a specialized case of PowerShell support.
  2. 托管PowerShell。您可以在应用中托管PowerShell代码,也可以从应用中访问PowerShell功能。这在某些情况下是有意义的,但也是PowerShell支持的特殊情况。

#1


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

在自定义应用程序中有多种方法可以利用PowerShell。 PowerShell自动化引擎的优点在于您无需处理处理参数,格式化输出以及您必须自己处理的其他PITA内容。要在PowerShell中将对象抛出管道,可以重写ProcessRecord方法并调用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);
}

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.

您可以编写允许管理员自动化应用程序服务器端的Cmdlet。 Cmdlet总是基于任务的功能单元,具有动词 - 名词命名约定。例如,Get-Process或Restart-Service。 Cmdlet做一件事并做得很好。将cmdlet组合在一起会带来各种令人难以置信的强大功能。

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:

此外,如果您的应用程序具有某种数据存储,则还可以编写一个提供程序,允许某人使用cmd(如cd(set-location)和md(new-item))浏览和/或管理数据存储。提供者是PS团队编写的,因此您可以使用cd hklm:或使用cd cert的证书存储区进入注册表:

You can also host PowerShell itself in your application.

您还可以在应用程序中托管PowerShell。

There is some good information on all three of these options on MSDN here

这里有关于MSDN上所有这三个选项的一些很好的信息

Considering the interest in providers, here are some examples on how to create a PowerShell Provider.

考虑到对提供程序的兴趣,以下是有关如何创建PowerShell提供程序的一些示例。

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.

有一些关于设计和使用一组cmdlet或者使用Provider公开某些内容的讨论。实现提供程序时,还会获得一些cmdlet,例如Get-item,New-item,Get-Location,Set-Location。但是,我发现除了提供程序之外,为特定任务设置一些cmdlet可能非常有用。

#2


Here's a good book on this side of powershell (which doesn't get much coverage in most books).

这是一本关于powershell这方面的好书(在大多数书中都没有得到很多报道)。

Professional Poweshell Programming

专业的Poweshell编程

Using this book (and some trips to msdn), I was able to have a custom powershell host up and runnning in a day. A few days later, I had custom cmdlets that interact with the gui (to build treeviews, grids, diagrams, menus, etc.)

使用这本书(以及对msdn的一些旅行),我能够在一天内完成并运行自定义PowerShell主机。几天后,我有了与gui交互的自定义​​cmdlet(用于构建树视图,网格,图表,菜单等)

#3


There are a couple of levels of PowerShell support, and not many people explain this well.

有几个级别的PowerShell支持,并没有多少人解释这一点。

  1. .NET classes. Yes, anything with a .NET API can be accessed by PowerShell without you doing anything extra! But, you don't get quite all of the extra PowerShell features. The least amount of work.
  2. .NET类。是的,PowerShell可以访问任何带有.NET API的内容,而无需您做任何额外的事情!但是,您没有获得所有额外的PowerShell功能。最少的工作量。

  3. A PowerShell module with cmdlets, scripts, and/or functions. This will require writing some PowerShell specific code to wrap around your existing API. If you go this route, you should sit down and learn the best practices for PowerShell design and think out your PowerShell support. This is more work required to do it right.
  4. 具有cmdlet,脚本和/或函数的PowerShell模块。这将需要编写一些PowerShell特定代码来包装您现有的API。如果你走这条路,你应该坐下来学习PowerShell设计的最佳实践,并考虑你的PowerShell支持。这是做正确的工作所需的更多工作。

  5. PowerShell providers. Providers are a special case, not everything should have a provider. You need to really consider whether your information really makes sense in a file system view.
  6. PowerShell提供商。提供商是一个特例,并非一切都应该有提供者。您需要在文件系统视图中真正考虑您的信息是否真的有意义。

This doesn't apply to your question, but I'll add it for completeness.

这不适用于您的问题,但我会将其添加为完整性。

  1. Hosted PowerShell. You can host PowerShell code in your app, or access PowerShell functionality from your app. This makes sense in some situations, but is also a specialized case of PowerShell support.
  2. 托管PowerShell。您可以在应用中托管PowerShell代码,也可以从应用中访问PowerShell功能。这在某些情况下是有意义的,但也是PowerShell支持的特殊情况。