使用自己的 PowerShell CmdLets 向我的 C# Windows 服务发送管理命令
我有一个运行服务的 C# Windows 应用程序。 我想利用 PowerShell 来提供命令行管理界面来管理我正在运行的服务。
从我的角度来看,我正在尝试提供与 Exchange 2007 相同的管理界面。
您对如何启动/设计管理 cmdlet 以便“连接”到正在运行的服务进行查询或发送有什么建议或示例代码吗?行政命令?
如何从 powershell 命令行访问服务的内部运行时状态? 例如,我想实现一个名为 Get-ConnectionsInfo 的 cmdlet,以了解执行该 cmdlet 时我的 Windows 服务正在使用多少个出站连接。
实际示例或示例的网络链接将非常受欢迎。
谢谢, 罗伯特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决方案是让您的 Windows 服务通过 WCF 公开管理界面。 您的 PowerShell 命令将使用此 WCF 服务来提取信息并使 Windows 服务执行操作。
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.
这里的关键是首先编写一个可以从 .Net 访问的 API,然后您可以轻松地将对此 API 的调用包装到一个 DLL 中,该 DLL 公开属于 PowerShell cmdlet 的类。 如果您希望有人能够远程管理您的服务,那么我认为最好的办法可能是创建一个 Web 服务或 WCF 服务来执行此操作,然后您可以将其包装在 PowerShell 中。 如果您查看 codeplex,您会发现一些管理基于远程 Web 的服务(例如 GoGrid 和 SQL 数据服务)的示例,它们将为您提供一些示例代码来帮助您入门。
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.
我想到了另一种公开 Windows 服务内部状态的方法。 您可以通过 WMI 公开信息,这些信息可以从 PowerShell 或 PowerShell 命令中使用。
我不确定您是否能够告诉您的服务通过 WMI 执行操作,但您至少能够提取状态。
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.
第三种替代方法可能是使 Windows 服务成为 PowerShell内容提供商。 不过,我自己从未制作过,所以我不确定它们是否适合托管在 Windows 服务中,因为该服务可能不会一直运行。
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.