0
votes

I am trying to get the first DNS server for a specific adapter.

Running: netsh interface ip show dnsservers name="local area connection" gives me the DNS server information for the "local area connection" adapter.

To that end I created the below (which works fine):

@echo on

FOR /F "skip=1 tokens=6" %%j in ('netsh interface ip show dnsservers^| findstr /i /c:"DNS Servers"') DO Set DNS=%%j echo %DNS%

But when running it against a specific adapter it fails. Code below:

@echo on

FOR /F "skip=1 tokens=6" %%j in ('netsh interface ip show dnsservers name="local area connection"^| findstr /i /c:"DNS Servers"') DO Set DNS=%%j echo %DNS%

Does anyone have an idea whats causing the issue?

1

1 Answers

0
votes

You have a problem with the equal sign in the netsh filter inside for command. It needs to be escaped

FOR /F "skip=1 tokens=6" %%j in (
  'netsh interface ip show dnsservers name^="local area connection"^| findstr /i /c:"DNS Servers"'
) DO Set DNS=%%j 
echo %DNS%