?
Solved

Set DNS only for static IP clients

Posted on 2015-02-05
3
Medium Priority
?
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85

Accepted Solution

by:
oBdA earned 1000 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 1000 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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Resolve DNS query failed errors for Exchange
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

764 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