解析 WMI DNS 主机名

发布于 2024-09-03 11:52:12 字数 1565 浏览 4 评论 0原文

我正在尝试对从 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(); <<这里说没找到。 有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-09-10 11:52:13

根据您所连接的操作系统,此属性将不可用。您可以从文档中看到它是在 Windows 2000 和 XP 上不可用。然而,它在 Win32_NetworkAdapterConfiguration 类上可用,但您将收到多个对象,您必须循环访问这些对象才能获取名称,因为大多数对象将为空。

此外,dnsHostName = oneName.Properties["DNSHostName"].ToString(); 不正确。它应该是 dnsHostName = oneName.Properties["DNSHostName"].Value.ToString()。再次强调,如果您决定使用 Win32_NetworkAdapterConfiguration,请记住它可以为 null。

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. 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.

Also, dnsHostName = oneName.Properties["DNSHostName"].ToString(); is not correct. It should be dnsHostName = oneName.Properties["DNSHostName"].Value.ToString(). Again, if you decide to use Win32_NetworkAdapterConfiguration keep in mind that it can be null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文