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).
wmic nicconfig list DNS /format:CSV- user6811411getmac /fo csv /vreturn? - user6811411netsh interface ip show dnsservers name="Local Area Connection 2"might also be useful - Dusan Bajicipconfig /alllook like in case there are two DNS servers? - aschipfl