我正在尝试从远程服务器检索网络适配器配置详细信息。我使用WMI在VB.NET中提出了以下代码。
执行代码(按钮点击)时,出现错误
"查询WMI数据时发生错误:未找到"。
请帮助我对这段代码出错的任何建议或更正?
Try
Dim path As ManagementPath = Nothing
Dim options As ConnectionOptions = Nothing
Dim scope As ManagementScope = Nothing
path = New ManagementPath("\\" & ServerName & "\root\cimv2")
options = New ConnectionOptions
options.Username = usernamebox.Text
options.Password = passwordbox.Text
scope = New ManagementScope(path, options)
scope.Connect()
Try
If scope.IsConnected = True Then
Dim nwquery As ObjectQuery = Nothing
nwquery = New ObjectQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = 'True'")
Dim searcher As ManagementObjectSearcher
searcher = New ManagementObjectSearcher(scope, nwquery)
Dim nwquerycol As ManagementObjectCollection
nwquerycol = searcher.Get
For Each nwobject As ManagementObject In nwquerycol
DataView_TextBox.Text &= "Name: " + nwobject("Name") & Newline
DataView_TextBox.Text &= "Name: " + nwobject("Description") & Newline
DataView_TextBox.Text &= "DHCPEnabled: " + (nwobject("DHCPEnabled").ToString) & ControlChars.NewLine
DataView_TextBox.Text &= "DHCPServer: " + nwobject("DHCPServer") & ControlChars.NewLine
DataView_TextBox.Text &= "DNSDomain: " + nwobject("DNSDomain") & ControlChars.NewLine
DataView_TextBox.Text &= "DNSHostName: " + nwobject("DNSHostName") & ControlChars.NewLine
If nwobject("IPAddress") Is Nothing Then
DataView_TextBox.Text &= "IPAddress:" + nwobject("IPAddress") & ControlChars.NewLine
Else
Dim arrIPAddress As String
arrIPAddress = nwobject("IPAddress")
For Each arrValue As String In arrIPAddress
DataView_TextBox.Text &= "IPAddress: {0}" + arrValue & ControlChars.NewLine
Next
End If
If nwobject("IPSubnet") Is Nothing Then
DataView_TextBox.Text &= "IPSubnet: " + nwobject("IPSubnet") & ControlChars.NewLine
Else
Dim arrIPSubnet As String
arrIPSubnet = nwobject("IPSubnet")
For Each arrValue As String In arrIPSubnet
DataView_TextBox.Text &= "IPSubnet: {0}" + arrValue & ControlChars.NewLine
Next
End If
DataView_TextBox.Text &= "MACAddress: {0}" + nwobject("MACAddress") & ControlChars.NewLine
DataView_TextBox.Text &= "======================================" & ControlChars.NewLine
Next
End If
Catch ex As Exception
MessageBox.Show("An error occurred while querying for WMI data: " & ex.Message)
End Try
Catch ex As Exception
MessageBox.Show("An error occurred while querying for WMI data: " & ex.Message)
End Try