BGINFO VB OR WMI Query to retrieve only active IPv4 address

Dantoys 0 Reputation points
2023-02-24T18:44:21.7133333+00:00

This question goes back to this request..

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/bb74c2eb-eca2-455d-a270-8dd0f3d195e6/wmi-query-to-retrieve-only-active-ipv4-address

I would like to do the same thing as this script, however I would only like the IP Address associated with the current Gateway. (Internet Gateway, I'm aware there could be custom routes)

Here is a Script I current got from the above thread.

strMsg = ""
strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'")

For Each IPConfig in IPConfigSet
 If Not IsNull(IPConfig.IPAddress) Then
 For i = LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
  If Not Instr(IPConfig.IPAddress(i), ":") > 0 Then
  strMsg = strMsg & IPConfig.IPAddress(i) & vbcrlf
  End If
 Next
 End If
Next

Echo strMsg
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,821 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
Windows Network
Windows Network
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Network: A group of devices that communicate either wirelessly or via a physical connection.
846 questions
Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,209 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 35,626 Reputation points
    2023-02-26T13:44:00.0933333+00:00

    Maybe this?

    strMsg = ""
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress,DefaultIPGateway from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'")
    
    For Each IPConfig in IPConfigSet
    	If Not IsNull(IPConfig.IPAddress) Then
    		If Not IsNull(IPConfig.DefaultIPGateway) Then
    			strMsg = strMsg & "Gateway = " & IPConfig.DefaultIPGateway(0) & vbcrlf 
    			For i = LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
    				If Not Instr(IPConfig.IPAddress(i), ":") > 0 Then
    					strMsg = strMsg & "IP = " & IPConfig.IPAddress(i) & vbcrlf
    				End If
    		Next
    		End if
    	End If
    Next
    
    wscript.Echo strMsg
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.