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

hnncsbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paolo SantiangeliConsulente InformaticoCommented:
anil_uCommented:
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
anil_uCommented:
Note the (i) in the following line
   Select Case IPConfig.DefaultIPGateway(i)
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

hnncsbAuthor Commented:
Thanks folks.  I missed the (i).
anil_uCommented:
glad we could help :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fncexpertsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.