• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7189
  • Last Modified:

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

0
hnncsb
Asked:
hnncsb
1 Solution
 
Paolo SantiangeliConsulente InformaticoCommented:
0
 
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
0
 
anil_uCommented:
Note the (i) in the following line
   Select Case IPConfig.DefaultIPGateway(i)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hnncsbAuthor Commented:
Thanks folks.  I missed the (i).
0
 
anil_uCommented:
glad we could help :)
0
 
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
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now