使用WMI实现,前提:知道对方IP,有效的用户名,密码。
public class ServiceControl
{
private string strPath;
private ManagementClass managementClass;
/// <summary>
/// 设置远程连接信息
/// </summary>
/// <param name="host">地址</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
public void Win32ServiceManager(string host, string userName, string password)
{
this.strPath = "" + host + "//root//cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if (userName != null && userName.Length > 0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope;
if (password == "")
{
managementScope = new ManagementScope("" + host + "//root//cimv2");
}
else
{
managementScope = new ManagementScope("" + host + "//root//cimv2", connectionOptions);
}
this.managementClass.Scope = managementScope;
}
}
/// <summary>
/// 验证是否能连接到远程计算机
/// </summary>
/// <param name="host">地址</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
public static bool RemoteConnectValidate(string host, string userName, string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope;
if (password == "")
{
managementScope = new ManagementScope("" + host + "//root//cimv2");
}
else
{
managementScope = new ManagementScope("" + host + "//root//cimv2", connectionOptions);
}
try
{
managementScope.Connect();
}
catch (Exception eee)
{
//throw eee;
}
return managementScope.IsConnected;
}
/// <summary>
/// 获取指定服务属性的值
/// </summary>
/// <param name="serviceName">服务名</param>
/// <param name="propertyName">属性名</param>
public object GetServiceValue(string serviceName, string propertyName)
{
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
return mo[propertyName];
}
/// <summary>
/// 获取所有服务属性的值
/// </summary>
public string[,] GetServiceList()
{
string[,] services = new string[this.managementClass.GetInstances().Count, 4];
int i = 0;
foreach (ManagementObject mo in this.managementClass.GetInstances())
{
services[i, 0] = (string)mo["Name"];
services[i, 1] = (string)mo["DisplayName"];
services[i, 2] = (string)mo["State"];
services[i, 3] = (string)mo["StartMode"];
i++;
}
return services;
}
/// <summary>
/// 获取所连接的计算机的指定服务数据
/// </summary>
/// <param name="serviceName">服务名</param>
public string[,] GetServiceList(string serverName)
{
return GetServiceList(new string[] { serverName });
}
/// <summary>
/// 获取所连接的计算机的指定服务数据
/// </summary>
/// <param name="serviceNames">服务名</param>
public string[,] GetServiceList(string[] serverNames)
{
string[,] services = new string[serverNames.Length, 4];
ManagementObject mo = this.managementClass.CreateInstance();
for (int i = 0; i < serverNames.Length; i++)
{
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serverNames[i] + "/"");
services[i, 0] = (string)mo["Name"];
services[i, 1] = (string)mo["DisplayName"];
services[i, 2] = (string)mo["State"];
services[i, 3] = (string)mo["StartMode"];
}
return services;
}
/// <summary>
/// 开启指定的服务
/// </summary>
/// <param name="serviceNames">服务名</param>
public string StartService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
try
{
if ((string)mo["State"] == "Stopped")//!(bool)mo["AcceptStop"]
mo.InvokeMethod("StartService", null);
}
catch (ManagementException e)
{
strRst = e.Message;
}
return strRst;
}
/// <summary>
/// 暂停指定的服务
/// </summary>
/// <param name="serviceNames">服务名</param>
public string PauseService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
try
{
//判断是否可以暂停
if ((bool)mo["acceptPause"] && (string)mo["State"] == "Running")
mo.InvokeMethod("PauseService", null);
}
catch (ManagementException e)
{
strRst = e.Message;
}
return strRst;
}
/// <summary>
/// 恢复指定的服务
/// </summary>
/// <param name="serviceNames">服务名</param>
public string ResumeService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
try
{
//判断是否可以恢复
if ((bool)mo["acceptPause"] && (string)mo["State"] == "Paused")
mo.InvokeMethod("ResumeService", null);
}
catch (ManagementException e)
{
strRst = e.Message;
}
return strRst;
}
/// <summary>
/// 停止指定的服务
/// </summary>
/// <param name="serviceNames">服务名</param>
public string StopService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
try
{
//判断是否可以停止
if ((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
mo.InvokeMethod("StopService", null);
}
catch (ManagementException e)
{
strRst = e.Message;
}
return strRst;
}
/// <summary>
/// 修改指定的服务启动方式
/// </summary>
/// <param name="serviceNames">服务名</param>
/// <param name="startMode">启动方式:Automatic,Manual,Disabled</param>
public string changeStartMode(string serviceName, string startMode)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath + ".Name=/"" + serviceName + "/"");
try
{
object[] ob = new object[1];
ob[0] = startMode;
mo.InvokeMethod("ChangeStartMode", ob);
}
catch (ManagementException e)
{
strRst = e.Message;
}
return strRst;
}
}