WIN 7 .bat file to change IP address .. DHCP problem

I have a few batch files that I use to switch NIC settings.  These are all "Run as Administrator".

For static IP, I use this:
@ECHO OFF
set varip=192.168.0.99
set varsm=255.255.255.0
set vargw=192.168.0.1
set vargwmetric=1
set vardns1=192.168.0.1
set vardns2=8.8.8.8
set vardnsindex1=1
set vardnsindex2=2

REM ***** You don’t need to change anything below this line! ******
ECHO Setting IP, mask, gateway and DNS
REM netsh set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
netsh interface ip set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
ECHO Setting Primary DNS
netsh interface ip set dns name="Local Area Connection" source=static address=none
ECHO Setting primary DNS
netsh interface ip add dns  name="Local Area Connection" %vardns1% index=%vardnsindex1%
ECHO Setting secondary DNS
netsh interface ip add dnsserver "Local Area Connection" %vardns2% index=%vardnsindex2%
ECHO Here are the new settings for %computername%:
netsh int ip show config
pause
end

Open in new window


To switch to DHCP "get an address automatically", I use this:
@ECHO OFF
REM ***** You don’t need to change anything below this line! ******
netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dns name="Local Area Connection" dhcp
netsh interface ip set default gateway name="Local Area Connection" dhcp
netsh interface ip show config[embed=file 906425]
pause
end

Open in new window

The problem is, that when I switch from a static address to a DHCP configuration, the default gateway IP address remains and the DHCP button isn't turned on - and the NIC settings dialog appears to disagree with the results of ipconfig that says DHCP YES!

ipconfig
At the same time, the NIC ipv4 settings look like this:
NIC settings
What might be the problem here?
LVL 27
Fred MarshallPrincipalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
netsh interface ip set address name="Local Area Connection" dhcp

You have name="Local Area Connection" but not source=dhcp

Did you try it like that?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic Advisor Commented:
Instead of ip - you should use ipv4, the command would be:

netsh interface ipv4 set address name="Local Area Connection" source=dhcp

Open in new window

The above command will switch DNS to automatically as well.

https://technet.microsoft.com/en-us/library/cc731521%28v=ws.10%29.aspx#BKMK_setaddress
0
Fred MarshallPrincipalAuthor Commented:
I tried those things but nothing in the results has changed.

ipconfig result and the GUI result are different re: DHCP.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Gerwin Jansen, EE MVETopic Advisor Commented:
This is Windows 7 right? I tested on W7 64bit machine and it works just fine? Any error message you see in Windows event log maybe?
0
Fred MarshallPrincipalAuthor Commented:
reboot...  :-(
Everything works now.
Thanks all!
0
Fred MarshallPrincipalAuthor Commented:
Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DHCP

From novice to tech pro — start learning today.