wmi控制遠端電腦的服務

來源:互聯網
上載者:User
using System;using System.Management;namespace ZZ.Wmi{     public class Win32ServiceManager     {         private string strPath;         private ManagementClass managementClass;         public Win32ServiceManager():this(".",null,null)         {         }         public 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 = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;                   this.managementClass.Scope = managementScope;              }         }          // 驗證是否能串連到遠端電腦         public static bool RemoteConnectValidate(string host,string userName,string password)         {              ConnectionOptions connectionOptions = new ConnectionOptions();              connectionOptions.Username = userName;              connectionOptions.Password = password;              ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;              try              {                   managementScope.Connect();              }              catch              {              }              return managementScope.IsConnected;         }         // 擷取指定服務屬性的值         public object GetServiceValue(string serviceName,string propertyName)         {              ManagementObject mo = this.managementClass.CreateInstance();              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");              return mo[propertyName];         }         // 擷取所串連的電腦的所有服務資料         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;         }         // 擷取所串連的電腦的指定服務資料         public string [,] GetServiceList(string serverName)         {              return GetServiceList(new string []{serverName});         }         // 擷取所串連的電腦的的指定服務資料         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;         }         // 停止指定的服務         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;         }         // 暫停指定的服務         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;         }         // 恢複指定的服務         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;         }         // 停止指定的服務         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;         }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.