Solved

Set DNS only for static IP clients

Posted on 2015-02-05
3
103 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Trying to figure out group policy inheritance and which settings apply where can be a chore.  Here's a very simple summary I've written which might help.  Keep in mind, this is just a high-level conceptual overview where I try to avoid getting bogge…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

10 Experts available now in Live!

Get 1:1 Help Now