Resolving the WMI DNS Host Name

Posted by Stephen Murby on Stack Overflow See other posts from Stack Overflow or by Stephen Murby
Published on 2010-05-28T14:03:42Z Indexed on 2010/05/28 20:22 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about beginner