2017-09-27 37 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群集。我需要写什么来做这件事? –