使用 ManagementObject 重启远程服务
我想在远程计算机上重新启动服务,并且不想使用 ServiceController,因为获取该计算机上所有服务的过程花费了 21 秒,而以下 ManagementObject 在不到 2 秒内返回:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + ConfigurationManager.AppSettings["remoteMachine"] + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_Service where DisplayName LIKE '%" + ConfigurationManager.AppSettings["likeSerices"] + "%'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<ServiceObj> outList = new List<ServiceObj>();
foreach (ManagementObject m in queryCollection)
{
ServiceObj thisObject = new ServiceObj();
thisObject.DisplayName = m["DisplayName"].ToString();
thisObject.Name = m["Name"].ToString();
thisObject.Status = m["State"].ToString();
thisObject.StartMode = m["StartMode"].ToString();
outList.Add(thisObject);
}
我现在尝试:m.InvokeMethod("停止服务”,空);在 foreach 块中没有成功。我在做什么?
谢谢 杰克
I want to restart a service on a remote machine and do not want to use ServiceController because the process to get all services on that machine took 21 seconds while the following ManagementObject returned in less than 2 seconds:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + ConfigurationManager.AppSettings["remoteMachine"] + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_Service where DisplayName LIKE '%" + ConfigurationManager.AppSettings["likeSerices"] + "%'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<ServiceObj> outList = new List<ServiceObj>();
foreach (ManagementObject m in queryCollection)
{
ServiceObj thisObject = new ServiceObj();
thisObject.DisplayName = m["DisplayName"].ToString();
thisObject.Name = m["Name"].ToString();
thisObject.Status = m["State"].ToString();
thisObject.StartMode = m["StartMode"].ToString();
outList.Add(thisObject);
}
I now tried:m.InvokeMethod("StopService", null); in the foreach block with no success. What am I doing worng?
Thank you
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不懂 C#,但 此处 的这个 VBScript 示例应该不太懂转换不好:
' 启动/停止服务的示例 WMI 脚本结束
I don't know C# but this VBScript sample from here shouldn't be too bad to convert:
' End of Example WMI script to Start / Stop services