我正在尝试对从AD检索到的计算机名称和我希望从计算机使用WMI的DNS主机名进行比较。
我目前有:
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");
}
}
}
}
但是我收到了一个ManagementException>> dnsHostName = oneName.Properties [“DNSHostName”]。ToString(); <<这里说没找到。 有什么想法吗?
答案 0 :(得分:1)
根据您连接到此属性的操作系统,将无法使用。您可以从documentation看到它在Windows 2000和XP上不可用。但是,它可以在Win32_NetworkAdapterConfiguration类中使用,但是您将收到多个对象,您必须循环才能获取名称,因为大多数对象都将为null。
另外,dnsHostName = oneName.Properties["DNSHostName"].ToString();
不正确。它应该是dnsHostName = oneName.Properties["DNSHostName"].Value.ToString()
。同样,如果您决定使用Win32_NetworkAdapterConfiguration,请记住它可以为null。