使用 ManagementObject 重启远程服务

发布于 2024-12-06 11:08:17 字数 1116 浏览 1 评论 0原文

我想在远程计算机上重新启动服务,并且不想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2024-12-13 11:08:17

我不懂 C#,但 此处 的这个 VBScript 示例应该不太懂转换不好:

' VBScript Restart Service.vbs
' Sample script to Stop or Start a Service
' www.computerperformance.co.uk/
' Created by Guy Thomas December 2010 Version 2.4
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"

'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit

' 启动/停止服务的示例 WMI 脚本结束

I don't know C# but this VBScript sample from here shouldn't be too bad to convert:

' VBScript Restart Service.vbs
' Sample script to Stop or Start a Service
' www.computerperformance.co.uk/
' Created by Guy Thomas December 2010 Version 2.4
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"

'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit

' End of Example WMI script to Start / Stop services

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文