how to access romote windows service with Related ObjectQuery

倾然丶 夕夏残阳落幕 提交于 2019-12-22 17:39:55

问题


I want to control (start and stop) a windows service which is in the remote machine. I can connect that machine but I can't access to windows service. Here is my code. Can you help me?

    ConnectionOptions oConn = new ConnectionOptions();
    oConn.Username = "****";
    oConn.Password = "****";

    ManagementScope managementScope = new ManagementScope(@"\\***.***.***.***\root\CIMV2", oConn);
    managementScope.Connect();

  RelatedObjectQuery roq = new RelatedObjectQuery("Win32_Service.Name='KanAktarim'");
  ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(managementScope, roq);
  ManagementObjectCollection mbCollection = moSearcher.Get();

回答1:


            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
            moSearcher.Scope = managementScope;
            moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='KanAktarim'");
            ManagementObjectCollection mbCollection = moSearcher.Get();

            foreach (ManagementObject oReturn in mbCollection)
            {
                ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);
                ManagementBaseObject outParams = oReturn.InvokeMethod("StopService", null, null);
                string a = outParams["ReturnValue"].ToString();

                string state = oReturn.Properties["State"].Value.ToString().Trim();
            }


来源:https://stackoverflow.com/questions/7117877/how-to-access-romote-windows-service-with-related-objectquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!