user7253664
user7253664

Reputation: 63

Parsing second DNS from IPCONFIG /all, Windows batch

I'm quite new to batch files and I'm trying to do something decently advanced and am trying to figure out how to identify and parse the second line under DNS SERVERS in IPCONFIG /all. If the answer is quite advanced, I would appreciate it greatly if you could explain it thoroughly. Here is my code:

@echo off
setlocal enabledelayedexpansion
set adapter=Ethernet adapter Local Area Connection
set adapterfound=false

for /f "usebackq tokens=1-4 delims=:" %%f in (`ipconfig /all`) do (
    set item=%%f
    if /i "!item!"=="!adapter!" (
        set adapterfound=true
    ) else if not "!item!"=="!item:DNS Servers=!" if "!adapterfound!"=="true" (
        rem echo DNS: %%g
        set Globaldns=%%g
        set adapterfound=false
    )
)
for /f "tokens=1-2 delims= " %%m in ("%Globaldns%") do set Globaldns=%%m
echo DNS: %Globaldns%

If someone has two DNS servers set, I need a way to pull the second DNS address and store it in a second variable, the code above is able to pull the first DNS but I have not figured out a way to pull the second. Thank you for your help!!

EDIT: Another piece of information. This needs to be able to be run on anything from Windows Vista to Windows 10 and it needs to be able to target a specific connection (we are only reconfiguring Ethernet devices, no wireless) So we need to be able to use the adapter's connection name to parse (i.e. Local Area Connection,Ethernet).

Upvotes: 2

Views: 1494

Answers (2)

JosefZ
JosefZ

Reputation: 30103

LotPings is right, ipconfig output is difficult to parse. Parsing wmic output could be easier.

Take a look at Win32_NetworkAdapter class and Win32_NetworkAdapterConfiguration class and their wmic aliases NIC and NICCONFIG, respectively.

For the first identification, use wmic NIC get /VALUE. Note the /VALUE switch and notice the NetConnectionID property: name of the network connection as it appears in the Network Connections Control Panel program. Then, parse output from

 wmic NIC where "NetConnectionID = 'Local Area Connection'" get Index, MACAddress

to get Index property (index number of the Windows network adapter configuration. The index number is used when there is more than one configuration available) value to a varible, e.g. _index and use it as follows:

wmic NICCONFIG where "Index = %_index%" get /Value

For easier parsing, you can narrow output to only desired properties and change format to csv, for instance

set "_properties=DefaultIPGateway,DHCPServer,DNSServerSearchOrder,IPAddress,IPSubnet"
wmic NICCONFIG where "Index = %_index%" get %_properties% /format:CSV

Please notice and apply Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem.

Edit: fixed a mistake with path keyword:

  • NIC       is a wmic alias for path Win32_NetworkAdapter
  • NICCONFIG is a wmic alias for path Win32_NetworkAdapterConfiguration

so e.g. next commands represent the same ˙WQL˙ query:

wmic NICCONFIG                              where "Index = %_index%" get /Value
wmic path Win32_NetworkAdapterConfiguration where "Index = %_index%" get /Value

Upvotes: 1

user6811411
user6811411

Reputation:

Not a direct answer to the question but this powershell script might help, albeit I don't know if it will work back to vista.

gwmi Win32_NetworkAdapter -filter "netconnectionid is not null"|
  %{ gwmi Win32_NetworkAdapterConfiguration `
    -filter "interfaceindex=$([int]$_.interfaceindex)"|
      select -expandproperty DNSServerSearchOrder}

As I have only one dns server configured my result is proper but meaningless.

Upvotes: 1

Related Questions