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.
Now this sounds interesting.
Have any of you done such extensions that exposes functionality in your application?
If so, what kind of functionallity?
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.
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.
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.
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:
You can also host PowerShell itself in your application.
There is some good information on all three of these options on MSDN here
Considering the interest in providers, here are some examples on how to create a PowerShell Provider.
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.