C# 查询 Windows 服务
我一直在使用一个应用程序来查询远程服务器上运行的 Windows 服务并将计算机名称、服务名称和状态写入数据库。
但是,我也想尝试捕获启动类型(自动、手动、禁用)。我使用的服务控制器没有任何启动类型选项,因此我开始考虑使用管理类。这个类看起来拥有我需要的一切,但我不知道如何在我的远程服务器上使用它。对于服务控制器,我正在这样做:
ServiceController[] services = ServiceController.GetServices(serverIP);
foreach (ServiceController service in services)
{
var machine = service.MachineName;
var displayName = service.DisplayName;
var status = service.Status;
}
我在管理类中尝试了这个:
ManagementClass class1 = new ManagementClass(serverIP + "\\" + "Win32_Service");
foreach (ManagementObject ob in class1.GetInstances())
{
var machine = serverIP;
var displayName = ob.GetPropertyValue("Description");
var name = ob.GetPropertyValue("PathName");
var startMode = ob.GetPropertyValue("StartMode");
var status = ob.GetPropertyValue("State");
}
但是当然它不起作用。有人知道如何使用管理类从远程计算机获取服务吗?或者是否有另一种方法使用服务控制器来获取启动类型?
我还尝试将它们结合起来,并将管理类 foreach 语句放入服务控制器中,但它陷入了无限循环。
I have been using an application that queries Windows Services running on remote servers and writes the Machine Name, Service Name, and Status to a database.
However, I want to try and capture the startup type (Automatic, Manual, Disabled) as well. I was using a Service Controller which does not have any options for startup type so I started looking at using a Management Class. This class looks like it has everything I need but I don't know how to use it against my remotes servers. For the Service Controller, I was doing this:
ServiceController[] services = ServiceController.GetServices(serverIP);
foreach (ServiceController service in services)
{
var machine = service.MachineName;
var displayName = service.DisplayName;
var status = service.Status;
}
I tried this for the Management class:
ManagementClass class1 = new ManagementClass(serverIP + "\\" + "Win32_Service");
foreach (ManagementObject ob in class1.GetInstances())
{
var machine = serverIP;
var displayName = ob.GetPropertyValue("Description");
var name = ob.GetPropertyValue("PathName");
var startMode = ob.GetPropertyValue("StartMode");
var status = ob.GetPropertyValue("State");
}
But of course it didn't work. Anyone know how I can get the Services from a remote machine using the Management Class? Or is there another way using the Service Controller to get the startup type?
I also tried to combine them both and put the Management Class foreach statement inside the Service Controller but it got stuck in an endless loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要查找的信息可在 WMI 中找到。
在 PowerShell 中编写整个过程比在纯 C# 中容易得多。 WMI 代码在 C#(或 C++、VBScript)中很快就会变得非常混乱。此代码片段演示了从计算机列表中获取数据。要嵌入到 C# 中,只需使用 System.Management.Automation,并添加 PowerShell.Create().AddScript(...).Invoke()
希望这会有所帮助,
The information you're looking for is available in WMI.
It will be MUCH easier to write this whole thing in PowerShell than in Pure C#. WMI code gets very messy in C# (or C++, or VBScript), very quickly. This snippet demostrates getting the data from a list of computers. To embed in C#, simply use System.Management.Automation, and add PowerShell.Create().AddScript(...).Invoke()
Hope this helps,
上面的代码也有效。只需将其添加到管理类中即可
The same code above worked. Just add this to Management Class