获取远程机器上的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;
}
}
}
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步