views:

1380

answers:

5

Hi,

I have a C# Windows application which runs a service. I would like to leverage PowerShell in order to offer a command line management interface for administering my running service.

From my point of view, I am trying to offer the same administrative interface a la Exchange 2007.

Do you have any suggestion or sample code on how to start/design the management cmdlets in order "connect" to the running service to query or send administrative commands?

How can I access service's internal runtime state from powershell command line? For example I would like to implement a cmdlet called Get-ConnectionsInfo to find out how many outbound connections my Windows service is using when the cmdlet is executed.

A practical example or web link to an example would be more than welcomed.

Thanks, Robert

+1  A: 

A solution would be for your Windows Service to expose an administrative interface through WCF. Your PowerShell commands would use this WCF service to pull out information and make the Windows Service perform actions.

Manga Lee
A: 

Another way of exposing the internal state of the Windows Service came to mind. You could expose information through WMI which can be consumed from PowerShell or your PowerShell commands.

I'm not sure if you are able to tell your service to perform actions through WMI but you would at least be able to pull out state.

Manga Lee
A: 

What I am trying to figure out is whether there is some sort of Powershell built-in mechanism which allows "communication" between an assembly holding cmdlets which runs in PowerShell.exe's process and a different process (like a Windows service). After studying a little bit IIS management cmdlets I believe there is no such thing and implementation is left to developer choice (via WCF, WMI or such).

My first idea was PSHost interface implemented in my service and provided to CreateRunspace, but from what I understood from the MSDN docs is that applications implementing PSHost interface will allow them to be notified of different events by powershell hosted in that runspace. But this is not what I want in this case.

Robert Mircea
This is a great question. As a side note, it's recommended that you keep comments in the "Comments" section of your original question and refrain from adding Answers. Unless of course you are actually answering your question (which is fine and encouraged).
Scott Saad
It would be nice if StackOverflow would allow comments more than 300 characters. It is very hard to give a compelling comment within this limit. :-)
Robert Mircea
A: 

A third alternative might be to make the Windows Service be a PowerShell content provider. However I have never made one myself so I'm not sure if they're suitable for hosting in a Windows Service, since the service might not be running all the time.

Manga Lee
A: 

The key here is to start by writting an API that you can access from .Net, then you can easily wrap the calls to this API into a DLL that exposes classes that are PowerShell cmdlets. If you want someone to be able to administer your service remotely then I think probably the best thing is to create either a webservice or WCF Service that does this and then you can wrap that in PowerShell. If you have a look on codeplex you will find some examples of administer remote web based services like GoGrid and SQL Data Services that will give you some sample code to get you started.

Darren Gosbell