简体   繁体   English

解析WMI DNS主机名

[英]Resolving the WMI DNS Host Name

I am trying to make a comparison between a machine name i have retrieved from AD, and the DNS Host Name i want to get using WMI from the machine. 我试图在我从AD检索的计算机名称和我想从该计算机使用WMI的DNS主机名称之间进行比较。

I currently have: 我目前有:

foreach (SearchResult oneMachine in allMachinesCollected)
            {
                pcName = oneMachine.Properties["name"][0].ToString();
                ConnectionOptions setupConnection = new ConnectionOptions();
                setupConnection.Username = USERNAME;
                setupConnection.Password = PASSWORD;
                setupConnection.Authority = "ntlmdomain:DOMAIN";
                ManagementScope setupScope = new ManagementScope("\\\\" + pcName + "\\root\\cimv2", setupConnection);
                setupScope.Connect();

                ObjectQuery dnsNameQuery = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
                ManagementObjectSearcher dnsNameSearch = new ManagementObjectSearcher(setupScope, dnsNameQuery);
                ManagementObjectCollection allDNSNames = dnsNameSearch.Get();
                string dnsHostName;
                foreach (ManagementObject oneName in allDNSNames)
                {
                    dnsHostName = oneName.Properties["DNSHostName"].ToString();
                    if (dnsHostName == pcName)
                    {
                        shutdownMethods.ShutdownMachine(pcName, USERNAME, PASSWORD);
                        MessageBox.Show(pcName + " has been sent the reboot command");
                    }
                }
            }
        }

But i get a ManagementException >> dnsHostName = oneName.Properties["DNSHostName"].ToString(); 但是我得到了ManagementException >> dnsHostName = oneName.Properties [“ DNSHostName”]。ToString(); << here saying not found. <<在这里说找不到。 Any ideas? 有任何想法吗?

Depending on the operating system you are connecting to this property will not be available. 根据所连接的操作系统的不同,将无法使用该属性。 You can see from the documentation that it is not available on Windows 2000 and XP. 您可以从文档中看到它在Windows 2000和XP上不可用。 However, it is available on the Win32_NetworkAdapterConfiguration class, but you will receive more than one object, which you will have to loop over to get the name as most of them will be null. 但是,它在Win32_NetworkAdapterConfiguration类中可用,但是您将收到多个对象,您将不得不循环获取该对象,因为它们中的大多数将为null。

Also, dnsHostName = oneName.Properties["DNSHostName"].ToString(); 另外, dnsHostName = oneName.Properties["DNSHostName"].ToString(); is not correct. 是不正确的。 It should be dnsHostName = oneName.Properties["DNSHostName"].Value.ToString() . 它应该是dnsHostName = oneName.Properties["DNSHostName"].Value.ToString() Again, if you decide to use Win32_NetworkAdapterConfiguration keep in mind that it can be null. 同样,如果您决定使用Win32_NetworkAdapterConfiguration,请记住它可以为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM