Solved

Set DNS only for static IP clients

Posted on 2015-02-05
3
105 Views
Last Modified: 2015-05-01
We are upgrading our DNS servers and need to change all static IP clients to point to the new DNS servers.

My criteria:

- Change only the adapters that are named similar to "Local Area Connection".  I say similar because some adapters are named "Local Area Connection" and others have numbers suffixed similar to "Local Area Connection 5".  Some clients have dual NICs.  Some clients have VMware Network Adapters.

And

- Change only the clients whose Local Area Connection is set to static IP.

I have tried it with the code below and it doesn't work reliably.  It works only on the explicit adapter name and rather annoyingly, it goes through the For statement for every positive hit.  In my case, it cycled through four times since I have four NIC cards (two physical and two VMWare).

for /f "tokens=2 delims==" %%F in ('wmic nic where "NetConnectionStatus=2 and AdapterTypeId=0 and NetConnectionID like 'Local Area Connection'" get NetConnectionID /format:list') do set nicName=%%F
if "%nicName%" NEQ "" goto setDNS
:setDNS
for /f "tokens=3" %%a in ('netsh interface ip show config ^| find /i "DHCP Enabled"') do ( stuff )

Open in new window


My environment is Win7 and Active Directory 2008 transitioning to AD 2012.

I'm wondering if there is a better and more reliable method?
0
Comment
Question by:mcnuttlaw
  • 2
3 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40592634
Late now, but few thoughts. Perhaps you should look for all adapters like you are then check that have the wrong/old dns servers in, presumably anything with the "old" ones in are wrong as the new ones are already given out by DHCP?

You could also search for PhysicalAdapter being true (though vmware still shows this), NetEnabled being True which would get rid of vpn adapter etc.  Perhaps Manufacturer not equal to VMWare or Microsoft

Steve
0
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 40602842
Something like that?  Currently it will give you a list of NICs it would process; you can add your "stuff" in line 28.
@echo off
setlocal enabledelayedexpansion
set AdapterName=Local Area Connection
set TempFile1=%Temp%\%~n01.tmp
set TempFile2=%Temp%\%~n02.tmp
wmic.exe nic where "NetConnectionStatus=2 and AdapterTypeId=0 and NetConnectionID like '%%%AdapterName%%%'" get Index, NetConnectionID /format:list >"%TempFile1%"
for /f "tokens=1* delims==" %%a in ('type "%TempFile1%"') do (
	set %%a=%%b
	if "%%a"=="NetConnectionID" (
		echo Processing NIC '!NetConnectionID!', index !Index! ...
		wmic.exe nicconfig where "Index=!Index!" get DHCPEnabled /format:list >"%TempFile2%"
		for /f "tokens=2 delims==" %%d in ('type "%TempFile2%"') do (
			if /i "%%d"=="TRUE" (
				echo ... skipped, DHCP is enabled.
			) else (
				echo ... static IP, changing ...
				call :SetDNS "!NetConnectionID!"
			)
		)
	)
)
if exist "%TempFile1%" del "%TempFile1%"
if exist "%TempFile2%" del "%TempFile2%"
goto :eof
:SetDNS NetConnectionID
set NetConnectionID=%~1
echo Setting new DNS servers for NIC '%NetConnectionID%'
REM Stuff
goto :eof

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 40603092
This was what I started before I saw the above post.  I would say best not to check for DHCP as it is quite possible and common in some environments to have DHCP allocated IP addresses but DNS can be set to fixed ones.  This looks for specific DNS servers and with the combination here has skipped vmware, sonicwall vpn, 3g modem connctions, disconncted LAN cards etc.

@echo off
setlocal enabledelayedexpansion
set oldserver=8.8.8.8

for /f "tokens=2 delims==" %%A in ('wmic nic where "netconnectionstatus=2 and adaptertypeid=0 and PhysicalAdapter=1 and NetEnabled=1" get Index /format:value ^| find /v ""') do (
  set cardid=%%~A
  for /f "tokens=2 delims==" %%D in ('wmic nicconfig where "index=%%~A" get "DNSServerSearchOrder" /format:list ^| find "%oldserver%"') do (
     call :ChangeDNS !cardid! %%~D
  )
)
echo %date% %time% completed,
exit /b

:ChangeDNS
echo The card with index %~1 has DNS including the oldserver -- %~2

wmic nicconfig where index=24 get * /format:list | more

pause
exit /b

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is in response to a question (http://www.experts-exchange.com/Networking/Network_Management/Network_Analysis/Q_28230497.html) here at Experts Exchange. The Original Poster (OP) requires a utility that will accept a list of IP addresses …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now