0

I am trying to retrieve the network adapter configuration details from a remote server. I came up with the below code in VB.NET using WMI.

When the code is executed (button click), I get an error

"An error occurred while querying for WMI data: Not found".

Please help in any suggestions or corrections on where I am going wrong with this piece of code?

    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
Filburt
  • 17,626
  • 12
  • 64
  • 115
kay5
  • 57
  • 3
  • 6
  • 16
  • What output from `wmic path Win32_NetworkAdapterConfiguration get caption , ipenabled` command? – JosefZ Jul 03 '15 at 11:39
  • The command is successfully listing all the network adapter caption and the IPEnabled state (True/False) on the remote computer that I am trying to query via the program. – kay5 Jul 03 '15 at 11:48
  • First, you should turn on `Option Strict`. Not all the properties you are trying to get are defined for that class; see [Win32_NetworkAdapterConfiguration class](https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx). [This answer](http://stackoverflow.com/a/26080478/1070452) shows how to dump whatever is available to the output window – Ňɏssa Pøngjǣrdenlarp Jul 03 '15 at 12:12

0 Answers0