Resolving the WMI DNS Host Name
- by Stephen Murby
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.
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(); << here saying not found.
Any ideas?