2017-09-27 91 views
0

我需要在Windows Server上自動啓動的服務。 有人可以告訴我一種使用WMI獲取Windows Server狀態(聯機或脫機)的方法。在C#中使用WMI獲取Windows服務器狀態

預先感謝您

+0

檢查這裏的完整的例子https://msdn.microsoft.com/en-us/ library/mt703458(v = vs.85).aspx –

回答

0

請嘗試以下代碼,

string FullComputerName = "<Name of Remote Computer>"; 
      ConnectionOptions options = new ConnectionOptions(); 
      ManagementScope scope = new ManagementScope("\\\\" + FullComputerName + "\\root\\cimv2", options); 
      scope.Connect(); 
      ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalService"); 
      ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
      ManagementObjectCollection queryCollection = searcher.Get(); 
      foreach (ManagementObject queryObj in queryCollection) 
      { 
       Console.WriteLine("-----------------------------------"); 
       Console.WriteLine("Win32_TerminalService instance"); 
       Console.WriteLine("-----------------------------------"); 
       Console.WriteLine("Started: {0}", queryObj["Started"]); 
       Console.WriteLine("State: {0}", queryObj["State"]); 
       Console.WriteLine("Status: {0}", queryObj["Status"]); 
      } 

來源: - https://social.msdn.microsoft.com/Forums/vstudio/en-US/a25d3071-2283-41c6-9262-6860d7965963/how-to-check-remote-servers-terminal-status-using-c?forum=csharpgeneral

+0

我從來沒有做過這樣的事情。我想連接到我的Windows Server上的Hyper-V羣集。我需要寫什麼來做這件事? –