获取远程机器上的windows服务 完全有效已测试


using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.ServiceProcess; using System.Text; using System.Threading.Tasks;   namespace ConsoleApp2 { class Program { static void Main(string[] args) { //try //{ // ServiceController ctrl = new ServiceController(); // ctrl.MachineName = "192.168.1.10"; // ctrl.ServiceName = "BFE"; // Console.WriteLine(ctrl.Status.ToString()); //} //catch(Exception ex) //{ // Console.WriteLine(ex.Message); //}   //Console.Read();   ConnectionOptions op = new ConnectionOptions(); //op.Username = "WIN-50QHRP5SDUI\\administrator"; //Remote Mangerment User //op.Password = "RongDa2018"; op.Username = "WIN-50QHRP5SDUI\\administrator"; op.Password = "RongDa2018"; ManagementScope scope = new ManagementScope(@"\\WIN-50QHRP5SDUI\root\cimv2", op); scope.Connect();   ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_Service ");//Where Name like 'Windows%' ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq); //获取WMI操作内容 ManagementObjectCollection queryCollection1 = query1.Get(); //根据使用者选择执行相应的远程操作 int i = 0; foreach (ManagementObject service in queryCollection1) { Console.WriteLine(i++ + "\t" + service.GetPropertyValue("Name").ToString() + "\t" + service.GetPropertyValue("Caption").ToString() + "\tab" + service.GetPropertyValue("State").ToString()); }     ManagementPath path = new ManagementPath("Win32_Service"); ManagementClass services; services = new ManagementClass(scope, path, null);   foreach (ManagementObject service in services.GetInstances()) { //Console.WriteLine(i++ +"\tab"+ service.GetPropertyValue("Name").ToString() + "\tab" + service.GetPropertyValue("State").ToString()); //if (service.GetPropertyValue("State").ToString().ToLower().Equals("running")) //{ // Do something }   //}   //return;   //try //{ // string pcname = "WIN-50QHRP5SDUI"; // ConnectionOptions connectionOptions = new ConnectionOptions(); // connectionOptions.Username = pcname + "\administrator"; // connectionOptions.Password = "RongDa2018"; // //connectionOptions.Authority = "ntlmdomain:DOMAIN"; // ManagementScope managementScope = new ManagementScope("\\\\" + pcname + "\\root\\cimv2", connectionOptions); // managementScope.Connect();   // try // { // ServiceController ctrl = new ServiceController(); // ctrl.MachineName = pcname; // ctrl.ServiceName = "BFE"; // Console.WriteLine(ctrl.Status.ToString()); // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // }   // ////////string pcname = "192.168.1.81"; // ////////ImpersonationUtil.Impersonate("rd","9527", pcname); //DESKTOP-4VQ2TCU // ////////// code you want to execute as impersonated user.....   // ServiceController[] Services = ServiceController.GetServices(pcname); //192.168.1.20 // foreach (ServiceController sc in Services) // { // Console.WriteLine(sc.DisplayName); // }   // ImpersonationUtil.UnImpersonate(); //} //catch (Exception ex) //{ // Console.WriteLine(ex.Message); //}   //Console.Read(); } Console.Read();   ///// ///// 获取windows 服务状态 ///// /////服务名称 /////服务器IP ///// //public ServiceControllerStatus GetServiceState(string serviceName, string serverIP) //{ // ServiceControllerStatus serviceSate; // using (ServiceController sc = ServiceController.GetServices(serverIP) // .FirstOrDefault(x => x.ServiceName == serviceName)) // { // if (sc == null) // { // throw new Exception($"{serviceName}不存在于{serverIP}"); // } // serviceSate = sc.Status; // sc.Close(); // } // return serviceSate; //} }     public String getServices() { string s = ""; ManagementObjectCollection itemCollection2 = new ManagementObjectSearcher("SELECT * FROM Win32_Service").Get(); foreach (ManagementObject MO in itemCollection2) { String displayName = (String)MO["Caption"]; s += displayName + " "; } return s; } } }