2012-03-07 24 views
0

考虑所有基于Windows的计算机的域。从这些计算机我想只检测Windows Server 2003计算机。如何仅检测来自域的Windows 2003计算机

我拥有域中所有机器的所有机器名称。那么如何从机器名称中确定OS版本。我用Environment.OSVersion来获得本地计算机的操作系统版本。如果我知道远程计算机名称,我如何获得远程计算机的操作系统版本?

+0

除了我已经提供了答案的事实之外,为什么这次投票结束(“脱离主题”)呢?这听起来像是一个合理的问题。 – 2012-03-07 13:23:23

回答

0

可以使用WMI(Windows Management Instrumentation)访问远程计算机的 Win32_OperatingSystem类实例。

using (var mc = new ManagementClass(@"\\" + computerName + @"\root\cimv2:Win32_OperatingSystem")) 
{ 
    foreach (var obj in mc.GetInstances()) 
    { 
     if (((bool)obj.Properties["Primary"].Value)) 
     { 
      int productType = (int)obj.Properties["ProductType"].Value; 
      string version = obj.Properties["Version"].Value.ToString(); 
      bool isServer = (productType == 2 || productType == 3); // "Domain Controller" or "Server 

      if (version == "5.2.3790" && isServer) 
      { 
      // "Caption" should contain something like "Microsoft(R) Windows(R) Server 2003..." 
      // Please resist parsing that, however.     
      Console.WriteLine(obj.Properties["Caption"].Value); 
      } 
     } 
    } 
} 

有关属性和什么样的价值观的细节都可以,请参阅Win32_OperatingSystem类的MSDN页面。

+0

谢谢。但我不明白是什么意思:properties [“Primary”]。value – sailer 2012-03-07 13:20:06

+0

是否意味着语法结构或特殊属性值“Primary”? – 2012-03-07 13:22:38

+0

:属性值“primary” – sailer 2012-03-07 13:25:44

相关问题