Jump to content

Subscript used with non-Array variable?


Recommended Posts

Hello,

I'm trying to pop open a window with the NIC and TCP/IP settings.

Here is the code:

$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
$collection = $objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")


For $obj In $collection
    If $obj.DHCPEnabled = -1 Then
        $DHCPStatus = "Yes"
    Else
        $DHCPStatus = "No"
    EndIf

    $RAWModel = $obj.Caption
    $IP = $obj.IPAddress
    $Subnet = $obj.IPSubnet
    $Gateway = $obj.DefaultIPGateway
    $DNS = $obj.DNSServerSearchOrder
    $DNSDOMAIN = $obj.DNSDomainSuffixSearchOrder
    $WINS1 = $obj.WINSPrimaryServer
    $WINS2 = $obj.WINSSecondaryServer
    $MAC = $obj.MACAddress
    $Model = StringMid($RAWModel, 12)

    MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & _
            "Model: " & $Model & @CRLF & _
            "IP Address: " & $IP[0] & @CRLF & _
            "Subnet Mask: " & $Subnet[0] & @CRLF & _
            "Gateway: " & $Gateway[0] & @CRLF & _
            "DNS Servers: " & $DNS[0] & ", " & $DNS[1] & @CRLF & _
            "Suffix Search Order: " & $DNSDOMAIN[0] & ", " & $DNSDOMAIN[1] & @CRLF & _
            "WINS Servers: " & $WINS1 & ", " & $WINS2 & @CRLF & _
            "MAC Address: " & $MAC)

Next

Within SciTE it runs but I see an error in the output window:

C:\Display Network Info.au3 (28) : ==> Subscript used with non-Array variable.:
MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & "Model: " & $Model & @CRLF & "IP Address: " & $IP[0] & @CRLF & "Subnet Mask: " & $Subnet[0] & @CRLF & "Gateway: " & $Gateway[0] & @CRLF & "DNS Servers: " & $DNS[0] & ", " & $DNS[1] & @CRLF & "Suffix Search Order: " & $DNSDOMAIN[0] & ", " & $DNSDOMAIN[1] & @CRLF & "WINS Servers: " & $WINS1 & ", " & $WINS2 & @CRLF & "MAC Address: " & $MAC)
MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & "Model: " & $Model & @CRLF & "IP Address: " & $IP[0] & @CRLF & "Subnet Mask: " & $Subnet[0] & @CRLF & "Gateway: " & $Gateway^ ERROR

So I thought ok, change this:

"Gateway: " & $Gateway[0] & @CRLF & _
to this
"Gateway: " & $Gateway & @CRLF & _

That fixes the error, but then the Gateway come back blank.

Can anyone point out what I'm doing wrong?

Thanks,

-Mike

Link to comment
Share on other sites

Hello,

I'm trying to pop open a window with the NIC and TCP/IP settings.

Here is the code:

$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
$collection = $objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For $obj In $collection
    If $obj.DHCPEnabled = -1 Then
        $DHCPStatus = "Yes"
    Else
        $DHCPStatus = "No"
    EndIf

    $RAWModel = $obj.Caption
    $IP = $obj.IPAddress
    $Subnet = $obj.IPSubnet
    $Gateway = $obj.DefaultIPGateway
    $DNS = $obj.DNSServerSearchOrder
    $DNSDOMAIN = $obj.DNSDomainSuffixSearchOrder
    $WINS1 = $obj.WINSPrimaryServer
    $WINS2 = $obj.WINSSecondaryServer
    $MAC = $obj.MACAddress
    $Model = StringMid($RAWModel, 12)

    MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & _
            "Model: " & $Model & @CRLF & _
            "IP Address: " & $IP[0] & @CRLF & _
            "Subnet Mask: " & $Subnet[0] & @CRLF & _
            "Gateway: " & $Gateway[0] & @CRLF & _
            "DNS Servers: " & $DNS[0] & ", " & $DNS[1] & @CRLF & _
            "Suffix Search Order: " & $DNSDOMAIN[0] & ", " & $DNSDOMAIN[1] & @CRLF & _
            "WINS Servers: " & $WINS1 & ", " & $WINS2 & @CRLF & _
            "MAC Address: " & $MAC)

Next

Within SciTE it runs but I see an error in the output window:

C:\Display Network Info.au3 (28) : ==> Subscript used with non-Array variable.:
MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & "Model: " & $Model & @CRLF & "IP Address: " & $IP[0] & @CRLF & "Subnet Mask: " & $Subnet[0] & @CRLF & "Gateway: " & $Gateway[0] & @CRLF & "DNS Servers: " & $DNS[0] & ", " & $DNS[1] & @CRLF & "Suffix Search Order: " & $DNSDOMAIN[0] & ", " & $DNSDOMAIN[1] & @CRLF & "WINS Servers: " & $WINS1 & ", " & $WINS2 & @CRLF & "MAC Address: " & $MAC)
MsgBox(1, "Network Info", "DHCP Enabled: " & $DHCPStatus & @CRLF & "Model: " & $Model & @CRLF & "IP Address: " & $IP[0] & @CRLF & "Subnet Mask: " & $Subnet[0] & @CRLF & "Gateway: " & $Gateway^ ERROR

So I thought ok, change this:

"Gateway: " & $Gateway[0] & @CRLF & _
to this
"Gateway: " & $Gateway & @CRLF & _

That fixes the error, but then the Gateway come back blank.

Can anyone point out what I'm doing wrong?

Thanks,

-Mike

You only get an array if there is a value set. Test for IsArray() on several of those parameters:
$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
$collection = $objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")


For $obj In $collection
    If $obj.DHCPEnabled = -1 Then
        $DHCPStatus = "Yes"
    Else
        $DHCPStatus = "No"
    EndIf

    $RAWModel = $obj.Caption
    $IP = $obj.IPAddress
    $Subnet = $obj.IPSubnet
    $Gateway = $obj.DefaultIPGateway
    $DNS = $obj.DNSServerSearchOrder
    $DNSDOMAIN = $obj.DNSDomainSuffixSearchOrder
    $WINS1 = $obj.WINSPrimaryServer
    $WINS2 = $obj.WINSSecondaryServer
    $MAC = $obj.MACAddress
    $Model = StringMid($RAWModel, 12)


    $sMsg = "DHCP Enabled: " & $DHCPStatus & @CRLF & _
            "Model: " & $Model & @CRLF
    
    If IsArray($IP) Then
        For $n = 0 To UBound($IP) - 1
            $sMsg &= "IP Address: " & $IP[$n] & @CRLF
        Next
    Else
        $sMsg &= "IP Address: <none>" & @CRLF
    EndIf
    $sMsg &= "Subnet Mask: " & $Subnet[0] & @CRLF
    
    
    If IsArray($Gateway) Then
        For $n = 0 To UBound($Gateway) - 1
            $sMsg &= "Gateway: " & $Gateway[$n] & @CRLF
        Next
    Else
        $sMsg &= "Gateway: <none>" & @CRLF
    EndIf
    
    If IsArray($Gateway) Then
        For $n = 0 To UBound($Gateway) - 1
            $sMsg &= "Gateway: " & $Gateway[$n] & @CRLF
        Next
    Else
        $sMsg &= "Gateway: <none>" & @CRLF
    EndIf

    If IsArray($DNS) Then
        For $n = 0 To UBound($DNS) - 1
            $sMsg &= "DNS Server: " & $DNS[$n] & @CRLF
        Next
    Else
        $sMsg &= "DNS Server: <none>" & @CRLF
    EndIf

    If IsArray($DNSDOMAIN) Then
        For $n = 0 To UBound($DNSDOMAIN) - 1
            $sMsg &= "DNS Suffix Search Order: " & $DNSDOMAIN[$n] & @CRLF
        Next
    Else
        $sMsg &= "DNS Suffix Search Order: <none>" & @CRLF
    EndIf
    
    If $WINS1 = "" Then $WINS1 = "<none>"
    If $WINS2 = "" Then $WINS2 = "<none>"
    $sMsg &= "Primary WINS Server: " & $WINS1 & @CRLF & _
            "Secondary WINS Server: " & $WINS2 & @CRLF & _
            "MAC Address: " & $MAC
    MsgBox(64, "Network Info", $sMsg)
Next

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Is there anyway to modify the solution above so that it does not cycle through all the NIC cards?

All I really need is the first one found, not the subsequent ones...which in my case, includes VMWare Virtual Adapters.

Thanks for any suggestions.

-Mike

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...