2014-08-27 109 views
1

我正在嘗試編寫一個程序,該程序將連接到遠程服務器並顯示服務的某種狀態。該服務也由我們(我的公司)編寫。無法在計算機上打開服務控制管理器

爲此我寫了一個控制檯應用程序和代碼是

static void Main(string[] args) 
    { 
     ConnectionOptions options = new ConnectionOptions(); 
     options.Password = "mypassword"; 
     options.Username = "Administrator"; 
     options.Impersonation = 
      System.Management.ImpersonationLevel.Impersonate; 

     ManagementScope scope = 
      new ManagementScope(
      "\\\\ip_of_the_server\\root\\cimv2", options); 
     scope.Connect(); 

     ServiceController svc = new ServiceController("My_Service_Name", "ip_of_the_server"); 

     var status = svc.Status.ToString(); 
     Console.WriteLine(svc.DisplayName + " : " status); 

    } 

但我不能使它發揮作用。我得到的錯誤是:

Cannot open Service Control Manager on computer 'ip_of_the_server'. This operation might require other privileges. 

內部異常:「訪問被拒絕」。

堆棧跟蹤:

at System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String  machineName, Int32 serviceControlManaqerAccess) 
    at System.ServiceProcess.ServiceController.GetDataBaseHandleWithConnectAccess() 
    at System.ServiceProcess.ServiceController.GenerateNames() 
    at System.ServiceProcess.ServiceController.get_ServiceName() 
    at System.ServiceProcess.ServiceController.GenerateStatus() 
    at System.ServiceProcess.ServiceController.get_Status() 
    at ServiceConsole.Program.Main(String[] args) in c:\Users\Kandroid\Documents\Visual Studio  2013\Projects\ServiceConsole\ServiceConsole\Program.cs:line 33 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

任何想法如何解決呢?

+0

使用具有正確權限的帳戶? – rene 2014-08-27 11:29:47

+0

:)是offcourse。我正在使用'管理員'。以及我需要什麼樣的權限? – kandroid 2014-08-27 11:31:09

+0

您設置了一個管理範圍,但是我沒有看到那個人去了您的ServiceController中的任何地方,對吧? – rene 2014-08-27 11:32:12

回答

2

正如你已經做了設置的管理範圍,只要使用WQL查詢正確的WMI對象WMI提供程序,在這種情況下WIN32_Service,這樣的事情:

var svc = new ManagementObjectSearcher(
    scope, 
    new ObjectQuery("Select Status,State from Win32_Service where Name='My_Service_Name'")) 
     .Get() 
     .GetEnumerator(); 

    if (svc.MoveNext()) 
    { 
      var status = svc.Current["Status"].ToString(); 
      var state = svc.Current["State"].ToString(); 
      Console.WriteLine("service status {0}", status); 
      // if not running, StartService 
      if (!String.Equals(state, "running",StringComparison.InvariantCultureIgnoreCase) { 
       ((ManagementObject) svc.Current).InvokeMethod("StartService", new object[] {}); 
      } 
    } 
    else 
    { 
      Console.WriteLine("service not found"); 
    } 

ManagementObjectSearcher負責檢索集合的WMI管理對象。 ObjectQuery將從範圍中返回類的實例。我們可以執行基本的sql select語句來選擇和項目結果集。

迭代器返回一個ManagementObjectBase,它具有Item訪問器以從返回的實例中檢索屬性。

+0

謝謝...您的代碼有效。但通過這種方式,我將如何啓動和停止服務? – kandroid 2014-08-27 11:51:06

+0

我添加了代碼來調用StartService操作 – rene 2014-08-27 12:12:25

+0

非常感謝..現在我可以從這裏... :) – kandroid 2014-08-27 12:13:49

相關問題