[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

Set DNS only for static IP clients

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
mcnuttlaw
Asked:
mcnuttlaw
  • 2
2 Solutions
 
Steve KnightIT ConsultancyCommented:
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
 
oBdACommented:
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
 
Steve KnightIT ConsultancyCommented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now