Link to home
Start Free TrialLog in
Avatar of hnncsb
hnncsbFlag for United States of America

asked on

Need to get default gateway via VBScript

Hello,

I am trying to get the default gateway of the active network adapter, then perform an action based on the gateway.  Right now I just want it to give me a message for testing.  I'm new to vbscript, so pardon my siimple question.  Here is the test code:


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.DefaultIPGateway) Then 
        For i=LBound(IPConfig.DefaultIPGateway) to UBound(IPConfig.DefaultIPGateway)
            WScript.Echo IPConfig.DefaultIPGateway(i)
            Select Case IPConfig.DefaultIPGateway
            	Case "10.5.1.1"
            		MsgBox "Your Gateway is 10.5.1.1"
            	Case "10.2.1.1"
            		MsgBox "Your Gateway is 10.1.1.1"
            	Case Else 
            End Select        
        Next
    End If
	Next

Open in new window

Avatar of Paolo Santiangeli
Paolo Santiangeli
Flag of Italy image

Avatar of anil_u
anil_u

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.DefaultIPGateway) Then
        For i=LBound(IPConfig.DefaultIPGateway) to UBound(IPConfig.DefaultIPGateway)
            WScript.Echo IPConfig.DefaultIPGateway(i)
            Select Case IPConfig.DefaultIPGateway(i)
                  Case "10.5.1.1"
                        MsgBox "Your Gateway is 10.5.1.1"
                  Case "10.1.1.1"
                        MsgBox "Your Gateway is 10.1.1.1"
                  Case Else
            End Select        
        Next
    End If
Next
Note the (i) in the following line
   Select Case IPConfig.DefaultIPGateway(i)
Avatar of hnncsb

ASKER

Thanks folks.  I missed the (i).
ASKER CERTIFIED SOLUTION
Avatar of anil_u
anil_u

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code runs well on most of my XP machines.
However I have one XP sp3 machine reporting:
Line:2
Char:1
Error: 0x80041014
Code: 80041014
Source: (null)

There's one internal nic, default gateway via dhcp..

Any ideas why this would fail?

Thanks